BoJack Horseman
BoJack Horseman

Reputation: 4452

Django url get request with parameter

I have following HTML file

<!DOCTYPE html>
<body>
  {% for champion in champions %}
    <a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>
  {% endfor %}
</body>
</html>

And these are my URLS

urlpatterns = patterns('',                                                                    url(r'^select_champion/$', views.select_champion, name='select_champion'),
  url(r'^guide_form/(?P<champion>\w+)/$', views.guide_form, name='guide_form'),
  url(r'^create_guide/$', views.create_guide, name='create_guide'),
  url(r'^get_guide/(?P<id>\d+)/$', views.get_guide, name='get_guide'),
  url(r'^guide_list/(?P<champion>\w+)/$', views.get_guide_list, name='get_guide_list'),                                 
)

When I try to select a champion I get following Error:

Reverse for 'guide_form' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['guides/guide_form/(?P\w+)/$']

When I change this line

<a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>

To this

<a href="{% url 'create_guide' %}{{champion}}">{{champion}}</a>

I don't get an Error but the wrong URL is called of course. I want to select a champion and want the champ to be delivered per url so a guide can be written about him in the guide form. Do you have any suggestion on how to solve my problem?

Upvotes: 0

Views: 181

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174614

Almost there, you need:

<a href="{% url 'guide_form' champion=champion %}">{{ champion }}</a>

Your url pattern has a keyword argument champion, and the value is the template variable {{ champion }}. The url tag understands the template context, so you don't need {{ }} around the variable; instead pass it in directly as an argument to the url tag.

Upvotes: 2

chandu
chandu

Reputation: 1063

It should be like this

<!DOCTYPE html>
<body>
  {% for champion in champions %}
    <a href="{% url 'guide_form' champion %} ">{{champion}}</a>
  {% endfor %}
</body>
</html>

Upvotes: 1

Related Questions