2017-08-22 69 views

回答

3

要做到这一点,您必须在所有模板中使用templatetags url

如果您urls.py你有这样的:

url(r'^articles/new$', NewArticleView.as_view(), name='new_article') 

而在你的模板,您有:

<a href="{% url 'new_article' %}">new article</a> 

把它转换为:

url(r'^(?P<username>.+/articles/new$', NewArticleView.as_view(), name='new_article') 

<a href="{% url 'new_article' request.user.username %}">new article</a> 

如果你不这样做使用templatetags url你没有别的办法(据我所知)比改变所有的模板

+0

我试图避免的是不得不改变我的模板中的所有'a href ='url。你的方法需要这个。 –

+0

好吧,我给了你下一次你想改变它们时避免重写你的所有网址的方法。 – user2021091