NoReverseMatch Error

231 views
Skip to first unread message

bahare hoseini

unread,
Jun 9, 2011, 3:28:30 AM6/9/11
to django...@googlegroups.com
hi there,
this is my views.py that doesn't have any problem:
 
from django.shortcuts import get_object_or_404, render_to_response
from django.core.urlresolvers import reverse
.
.
.
        return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))

def results(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('(...)/polls/results.html', {'poll': p})
----------------------------------------------------------------------------------------------------------------
but when i edit my urls.py like this:

#...
urlpatterns = patterns('',
    (r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='(...)polls/index.html')),
    (r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Poll,
            template_name='(...)polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='(...)polls/results.html'),
        name='poll_results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
------------------------------------------------------------------------------------------------------------------
and open my browser with the link (http://localhost:8000/polls/id=1/vote), I'd face this error: can anybody help please?
Reverse for 'polls.views.results' with arguments '(id=1,)' and keyword arguments '{}' not found.

can anybody help?


krzysiekpl

unread,
Jun 9, 2011, 3:46:09 PM6/9/11
to Django users
Hi,

Look at this:

https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse

`viewname` is incorrect in your app you must use 'poll_results'
instead of 'polls.views.results'


Krzysztof Hoffmann

Karen Tracey

unread,
Aug 13, 2012, 10:59:28 PM8/13/12
to django...@googlegroups.com
On Mon, Aug 13, 2012 at 10:46 PM, Syam Palakurthy <syampal...@gmail.com> wrote:
Hi - I could not find any explanation that fixed the problem, until I ran across this person's abridged Django tutorial: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial

It's basically a line in the details template, which should be:
<form action="/polls/{{ poll.id }}/vote/" method="post">

Instead of:
<form action="{% url 'polls.views.vote' poll.id %}" method="post">

I'm not sure why this fixed the issue, but it did for me.  I'd love an explanation if anyone has one.


This is not a typo in the tutorial. The {% url %} variant is a faily recent update to the current  development level of the online docs. It was added in order to show in the tutorial best practices: url reversal by name rather than hard-coding url paths in the template. However, it uses the {% url %} tag syntax that is correct for the upcoming Django release, 1.5. In order for that syntax (specifically, the quotes around polls.views.vote) to work in the 1.4 released version, the template would also have to include {% import url from future %}. This "from future" import is not necessary in the current development level of code, so was not included when this change to demonstrate how to use {% url %} was added to the tutorial development level. However it seems that many many many people are running through the development-level doc with the 1.4- released level code, so perhaps we should add a note here (or more prominently elsewhere) to please be sure you run through the online version of the tutorial that matches the level of code you are running.

Karen

Serge G. Spaolonzi

unread,
Aug 14, 2012, 10:46:52 AM8/14/12
to django...@googlegroups.com
Try removing the quotes from 'polls.views.vote':
<form action="{% url polls.views.vote poll.id %}" method="post">
Regards

-- 
Serge G. Spaolonzi
Cobalys Systems


On Mon, Aug 13, 2012 at 11:46 PM, Syam Palakurthy <syampal...@gmail.com> wrote:
Hi - I could not find any explanation that fixed the problem, until I ran across this person's abridged Django tutorial: http://tony.abou-assaleh.net/web-development/stripped-down-django-tutorial

It's basically a line in the details template, which should be:
<form action="/polls/{{ poll.id }}/vote/" method="post">

Instead of:
<form action="{% url 'polls.views.vote' poll.id %}" method="post">

I'm not sure why this fixed the issue, but it did for me.  I'd love an explanation if anyone has one.

Thanks,
Syam

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/2MW2j_RVlfYJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



Karen Tracey

unread,
Aug 15, 2012, 7:53:23 AM8/15/12
to django...@googlegroups.com
On Tue, Aug 14, 2012 at 10:46 AM, Serge G. Spaolonzi <se...@cobalys.com> wrote:
Try removing the quotes from 'polls.views.vote':
<form action="{% url polls.views.vote poll.id %}" method="post">


Note that will work for 1.4 but it's doing things the old way, and that will break in 1.5. Much better to add {% import url from future %} and have code that will continue to work with the next release of Django. Removing the quotes is not the best approach at this time.
 
Karen
--
http://tracey.org/kmt/

Syam Palakurthy

unread,
Aug 25, 2012, 1:28:23 AM8/25/12
to django...@googlegroups.com
Hi, Can you please elaborate on how to use this block in the template code?  I've tried adding this into my details template code in various places and cannot get it to successfully work.  Also, any suggestions/examples you could point me to would be much appreciated.

<h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}


{% import url from future %}
<form action="/polls/{{ poll.id }}/vote/" method="post">

{% csrf_token %}
{% for choice in poll.choice_set.all %}
    
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

Thanks,
Syam

Karen Tracey

unread,
Aug 25, 2012, 8:46:29 AM8/25/12
to django...@googlegroups.com
On Sat, Aug 25, 2012 at 1:28 AM, Syam Palakurthy <syampal...@gmail.com> wrote:
Hi, Can you please elaborate on how to use this block in the template code?  I've tried adding this into my details template code in various places and cannot get it to successfully work.  Also, any suggestions/examples you could point me to would be much appreciated.


Sorry, it's {% load url from future %}, not import. Brain misfired and put Python word in place of Django template language.

See the forward compatibility note here: https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url

Karen
--
http://tracey.org/kmt/

Reply all
Reply to author
Forward
0 new messages