Take a look at the named URLs, you can find django's official documentation here .
Basically you can name your urls in your url as such:
url(r'^user/(?P<user_id>\d+)/profile/$', 'yourapp.views.view', name='user_url')
And then in any template you can do this:
<a href="{% url user_url user.id %}">
However, this will make your URL structure pretty ugly, and there are better ways to do this. For example, you can simply go to / profile / and the userid will be retrieved from the current session (each request has a user attribute, use it). So, for example, in your opinion, you can do this:
def myview(request): user = request.user
And later you can use this information to do what you want. Much better than using identifiers in the URL, and you don't need to worry about any other security issues that may arise.
source share