<p>Django的<a href="https://docs.djangoproject.com/en/dev/misc/design-philosophies/" rel="noreferrer">core design philosophies</a>之一是<strong>url应该很漂亮。</strong></p>
<p>因此,像<strong><code>accounts/detail?name='abc'</code></strong>这样的url应该映射为<strong><code>accounts/detail/abc/</code></strong>。您可以在url配置中使用regex捕获它。这里的网址是相当整洁和用户友好。这将有助于搜索引擎正确索引您的页面(现在您可以忘记<code>rel=canonical</code>),并将有助于搜索引擎优化。</p>
<p>现在,考虑一个拖尾斜杠的原因,对于路径为<strong><code>about.html</code></strong>的用户来说,一个相对解析<strong><code>users/awesomeUser</code></strong>的视图(在任何框架中)</p>
<p>因为<code>users/awesomeUser</code>和<code>users/awesomeUser/</code>是不同的</p>
<ol>
<li><p>如果用户在<code>users/awesomeUser</code>,浏览器会将其解析为<code>users/about.html</code>,因为没有我们不想要的尾随斜杠</p></li>
<li><p>如果用户在<code>users/awesomeUser/</code>,浏览器会将其解析为<code>users/awesomeUser/about.html</code>,因为有一个尾随斜杠</p></li>
</ol>
<blockquote>
<ul>
<li><code>child</code> relative to <code>family/parent/</code> is <code>family/parent/child</code>.</li>
<li><code>child</code> relative to <code>family/parent</code> is <code>family/child</code>.</li>
</ul>
</blockquote>
<h3><a href="https://docs.djangoproject.com/en/1.10/misc/design-philosophies/#definitive-urls" rel="noreferrer">Django Design philosophy on Definitive URLs</a>读取,</h3>
<blockquote>
<p>Technically, <strong>foo.com/bar</strong> and <strong>foo.com/bar/</strong> are two different URLs, and search-engine robots (and some Web traffic-analyzing tools) would treat them as separate pages. Django should make an effort to “normalize” URLs so that search-engine robots don’t get confused.</p>
<p><strong>This is the reasoning behind the APPEND_SLASH setting.</strong> (APPEND_SLASH lets you force append slashes to a URL)</p>
</blockquote>
<h2>还是不相信?</h2>
<ol>
<li>由于django观察这两个url是不同的,如果您正在缓存应用程序,django将在<code>user/awesomeUser</code>和<code>user/awesomeUser/</code>为同一页保留两个副本。</li>
<li>如果不将斜杠附加到URL(如果您曾计划构建REST API),那么除了GET之外,HTTP方法还可能有其他问题。</li>
</ol>
<h2>更新</h2>
<p>除非在“设置”中显式定义<code>APPEND_SLASH=False</code>,并且为每个要使用的路由器(如果使用路由器)定义<code>trailing_slash=False</code>,否则无法使POST/PUT/PATCH/DELETE方法与<code>rest_framework</code>一起工作。<em>这就好像你基本上会跳过这个大部分时间,你得浪费很多时间调试这个。Django建议使用<code>append slashes</code>,但不强制使用。</p>
<p>由开发人员决定是否附加斜杠。</p>