D
D
Dominik092016-08-16 15:06:02
Django
Dominik09, 2016-08-16 15:06:02

How to correctly pass a parameter in Django to url?

I'm using the slug model to navigate to articles via titles in the address bar. I wrote everything in the URL as in the documentation:

urlpatterns = patterns('content.views',
    url(r'^publication/add/', 'add_publication', name = 'Add_publication'),
    url(r'^publication/(?P<slug>[a-z0-9_-]+)/$', 'get_publication', name = 'Get_publication'),
    url(r'^publication/edit/(?P<slug>[a-z0-9_-]+)/$', 'edit_publication', name = 'Edit_publication'),

The problem is that when I pass the parameter in the url like this:
<a href="{% url 'Edit_publication' publication.slug %}" class="btn btn-warning">Редактировать новость</a>

Error:
NoReverseMatch
Reverse for 'Edit_publication' with arguments '('text-2',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
If I write like this:
<a href="/publication/edit/{{ publication.slug }}" class="btn btn-warning">Редактировать новость</a>

then everything works without problems, but it’s wrong to do this throughout the site, as you understand. Can you explain what is the problem? I will add that I have tried methods like "{% url 'content:edit_publication' publication.slug %}" or "{% url 'Edit_publication' slug=publication.slug %}" but it does not help. I think it makes no sense to throw off View, because the parameters, based on the second method, accept and work without problems.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2016-08-16
@mututunus

Urls included in main urls.py?

V
Vladimir Kuts, 2016-08-16
@fox_12

{% url 'Edit_publication' slug=publication.slug %}
What outputs? slug is empty?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question