2017-08-22 82 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