1

I am using Django 3.1

I have URLs configured so that I can corretly use the following in my template:

<li><a href="{% url 'sitemember:profile' 'bobby' %}">{{ user.username }}&nbsp;&nbsp;</a></li>

Where user.username resolves to 'bobby'

How do I pass the variable user.username to the url() tag, so that the URL resolves correctly as:

https://localhost:/user/bobby (as an example)

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • Does this answer your question? [How to add url parameters to Django template url tag?](https://stackoverflow.com/questions/25345392/how-to-add-url-parameters-to-django-template-url-tag) – Lord Elrond Oct 29 '20 at 04:40

1 Answers1

2

You specify this in the {% url … %} template tag [Django-doc]:

<a href="{% url 'sitemember:profile' user.username %}">
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I get: `Reverse for 'profile' with arguments '('',)' not found. 1 pattern(s) tried: ['user\\/(?P[^/]+)\\/$'] ` – Homunculus Reticulli Oct 28 '20 at 22:40
  • @HomunculusReticulli: that is because the `username` is empty, or `user` does not exists in the template. This is what the error is about it says *arguments `('',)`*, so it has one parameter, but that resolved to the empty string. – Willem Van Onsem Oct 28 '20 at 22:41
  • Oh, I see. Thanks very much. That makes sense. The error message was a bit cryptic, but yes, it makes sense now that you explained it. – Homunculus Reticulli Oct 28 '20 at 22:42