2

How do you specify a named URL pattern in Django if you need to pass it a template variable? My template contains the following URL:

# templates/welcome.html
# Link to template that shows all members near member with this uid
<a href="{% url 'show-members-near' %}uid={{ uid }}">Show members near you</a>

My top-level URLconf file contains the following pattern which points to the application-specific url file:

# conf/urls.py
urlpatterns = patterns('',
    ...
    url(r'^members/',            include('apps.members.urls')),
    ...
)

Here is my application-specific url file:

# app/members/url.py
url(r'^near/(?P<uid>\d{1,})/$',
    'show_members',
    {'template': 'show_members.html'},
    name='show-members-near'),

This is the error I get:

NoReverseMatch at /personal_profile/welcome/
Reverse for 'show-members-near' with arguments '()' and keyword arguments '{}' not found.  1 pattern(s) tried: ['members/near/(?P<uid>\\d{1})/$']

I tried to insert a keyword argument like this but it didn't work:

<a href="{% url 'show-members-near' uid={{ uid }} %}">Show members near you</a>

After doing some research, everything I read seemed to indicate that the keyword argument should be passed the way I did it up above. What I'm trying to do is the equivalent of the following which does work:

<a href="/members/near/{{ uid }}/">See who's here</a>

How do I specify a keyword argument? I didn't see anything that addresses this situation in the Django docs (although I might have missed it).

Thanks!

2 Answers 2

4

You'd just pass the uid in as a keyword. You can find it documented here. So, your url call would look like the following:

<a href="{% url 'show-members-near' uid=uid %}">Show members near you</a>
Sign up to request clarification or add additional context in comments.

Comments

1

This case is, of course, explicitly supported and documented. You're missing the fact that everything inside a template tag is evaluated. You can just pass the uid in:

{% url 'show-members-near' uid=uid %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.