擅长:python、mysql、java
<p>这是一个老问题,但这里有一些东西可以帮助别人。</p>
<p>官方文件:</p>
<blockquote>
<p>Django provides tools for performing URL reversing that match the
different layers where URLs are needed: In templates: Using the url
template tag. In Python code: Using the reverse() function. In higher
level code related to handling of URLs of Django model instances: The
get_absolute_url() method.</p>
</blockquote>
<p>例如在模板中(url标记)</p>
<pre><code><a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
</code></pre>
<p>例如在python代码中(使用<code>reverse</code>函数)</p>
<pre><code>return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))
</code></pre>