brave-hawk
brave-hawk

Reputation: 311

Django prepend string to url

How do I prepend a string to all my urls? Say I have http://127.0.0.1:8000/articles/new/ I want to have http://127.0.0.1:8000/username/articles/new/ Without having to change all the template urls throughout my site. The string I want to prepend here is username based on logged in user. How to achieve this?

Upvotes: 0

Views: 438

Answers (1)

user2021091
user2021091

Reputation: 571

To do it first you have to use the templatetags url in all your templates.

If in your urls.py you have something like:

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

And in your templates you have:

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

Transform it to:

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

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

If you don't use the templatetags url you have no other way (in my knowledge) than changing all your templates

Upvotes: 3

Related Questions