Getting NoReverseMatch and can't find the mistake

66 views
Skip to first unread message

Bob Aalsma

unread,
Nov 17, 2015, 2:08:22 PM11/17/15
to Django users
Sorry, can't seem to find what's wrong here, please help: what am I missing???

I'm seeing

NoReverseMatch at /zoekopdrachten/4/resultaat/

Reverse for 'resultaat' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'zoekopdrachten/(?P<zoekvraag_id>[0-9]+)/resultaat/$']
Request Method:GET
Request URL:http://127.0.0.1:8000/zoekopdrachten/4/resultaat/
Django Version:1.8.5
Exception Type:NoReverseMatch
Exception Value:
Reverse for 'resultaat' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'zoekopdrachten/(?P<zoekvraag_id>[0-9]+)/resultaat/$']
Exception Location:/Library/Python/2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 495
Python Executable:/usr/bin/python
Python Version:2.7.10


with views.py:
"""
    zoekopdrachten: views.py
    
"""

from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.core.urlresolvers import reverse

from .models import KlantOrganisatie, ZoekVraag, ZoekVraagLocatie

#
#   Index - overzicht van opdrachtgever, contactpersonen en zoekvragen
def index(request):
    organisatie_lijst = KlantOrganisatie.objects.order_by('klant_organisatie_naam')
    context = {'organisatie_lijst': organisatie_lijst}
    return render(request, 'zoekopdrachten/index.html', context)


#
#   Klantverwerking - contactpersoon noteert oordeel en motivatie bij aangeboden zoekresultaten
def klantverwerking(request, zoekvraag_id):
    vraag = get_object_or_404(ZoekVraag, pk=zoekvraag_id)
    return render(request, 'zoekopdrachten/klantverwerking.html', {'vraag': vraag})
    

#
#   Resultaat - inlezen zoekresultaten en opnemen in database bij ZoekVraagResultaat
def resultaat(request, zoekvraag_id):
    vraag = get_object_or_404(ZoekVraag, pk=zoekvraag_id)
    try:
        keuze = vraag.zoekvraaglocatie_set.get(pk=request.POST['zoekvraaglocatie'])        
    except (KeyError, ZoekVraagLocatie.DoesNotExist):
        return render(request, 'zoekopdrachten/vraag.html', {
            'vraag': vraag,
            'error_message': 'Ongeldige keuze of zoiets',
        })        
    else:
        keuze.zoek_vraag_locatie = 'kippenfarm'
        keuze.save()
        return HttpResponseRedirect(reverse('zoekopdrachten:zelfverwerking.html', args=(vraag.id,)))


#
#   Vraag - details van de zoekvraag
def vraag(request, zoekvraag_id):
    vraag = get_object_or_404(ZoekVraag, pk=zoekvraag_id)
    return render(request, 'zoekopdrachten/vraag.html', {'vraag': vraag})


#
#   Zelfverwerking - opnemen zoekresultaten in databsae plus eigen beoordeling
def zelfverwerking(request, zoekvraag_id):
    vraag = get_object_or_404(ZoekVraag, pk=zoekvraag_id)
    return render(request, 'zoekopdrachten/zelfverwerking.html', {'vraag': vraag})

and urls.py:
""" 
    zoekopdrachten: urls.py
    
"""

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /zoekopdrachten/
    url(r'^$', views.index, name='index'),
    # ex: /zoekopdrachten/5/
    url(r'^(?P<zoekvraag_id>[0-9]+)/$', views.vraag, name='vraag'),
    # ex: /zoekopdrachten/5/zelfverwerking/
    url(r'^(?P<zoekvraag_id>[0-9]+)/zelfverwerking/$', views.zelfverwerking, name='zelfverwerking'),
    # ex: /zoekopdrachten/5/klantverwerking/
    url(r'^(?P<zoekvraag_id>[0-9]+)/klantverwerking/$', views.klantverwerking, name='klantverwerking'),
    # ex: /zoekopdrachten/5/resultaat/
    url(r'^(?P<zoekvraag_id>[0-9]+)/resultaat/$', views.resultaat, name='resultaat'),
]


and vraag.html:
<h1>Zoekvraag "{{ vraag.zoekvraag_id }}"</h1>

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

<h2>Verzamelen resultaten</h2>
<form action="{% url 'zoekopdrachten:resultaat' vraag.zoekvraag_id %}" method="post">
{% csrf_token %}
{% for term in vraag.zoekvraagterm_set.all %}
<input type="radio" name="term" id="term{{ forloop.counter }}" value="{{ term.id }}" />
<label for="choice{{ forloop.counter }}">1</label><br />
{% endfor %}
</br>
<input type="submit" value="Verwerken SourcingThing" />
</form>









Tom Evans

unread,
Nov 18, 2015, 9:32:53 AM11/18/15
to django...@googlegroups.com
On Tue, Nov 17, 2015 at 7:08 PM, Bob Aalsma <overhaalsg...@me.com> wrote:
> Sorry, can't seem to find what's wrong here, please help: what am I
> missing???
>
> I'm seeing
>
> NoReverseMatch at /zoekopdrachten/4/resultaat/
>
> Reverse for 'resultaat' with arguments '('',)' and keyword arguments '{}'
> not found. 1 pattern(s) tried:
> [u'zoekopdrachten/(?P<zoekvraag_id>[0-9]+)/resultaat/$']

The pattern asked for an argument, but no arguments were passed to
reverse() / {% url %}

> <snip>
> and vraag.html:
> <h1>Zoekvraag "{{ vraag.zoekvraag_id }}"</h1>
>
> {% if error_message %}
> <p><strong>{{ error_message }}</strong></p>
> </br>
> {% endif %}
>
> <h2>Verzamelen resultaten</h2>
> <form action="{% url 'zoekopdrachten:resultaat' vraag.zoekvraag_id %}"
> method="post">

and therefore the argument you are trying to pass here does not exist.
Debug your template context to determine why.

Cheers

Tom

Bob Aalsma

unread,
Nov 19, 2015, 2:04:05 PM11/19/15
to Django users
Really sorry Tom, I've been looking at your text and thinking about it for most of today, but no penny dropping. 
I'm rather unsure which bits go where, I'm trying to understand this by rebuilding the tutorial.

I can't seem to get my head around this 'reverse()' part: the reverse() as such is used in the HttpResponseRedirect statement whereas the error seems to be caused by the form statement in vraag.html - how do these things connect?

Daniel Roseman

unread,
Nov 19, 2015, 4:18:19 PM11/19/15
to Django users
Tom is mistaken, you *did* pass an argument in your url tag: `{% url 'zoekopdrachten:resultaat' vraag.zoekvraag_id %}`. However, as the error message says, the argument that you passed is being interpreted as the empty string, which doesn't match the url pattern.

You haven't posted your models, but I suspect that the reason for this is that there is no such attribute as `zoekvraag_id` on your Vraag model, so `vraag.zoekvraag_id` doesn't return a value. I think you probably meant `vraag.id`.
-- 
DR.

Bob Aalsma

unread,
Nov 19, 2015, 4:38:50 PM11/19/15
to Django users
Umm, one of the things I've found difficult is to determine the logic in the translation of the class names in models.py to other variables.

And I'm sorry to have not included the models.py. This is the part of ZoekVraag:

class ZoekVraag(models.Model):
    vrager = models.ForeignKey(KlantContactPersoon)
    vraag_naam = models.CharField("naam vraag", max_length=200)
    vraag_omschrijving = models.TextField("omschrijving vraag", blank = True)
    vraag_taktiek = models.TextField("taktiek oplossing vraag", blank = True)
    
    def __unicode__(self):
        return self.vraag_naam
        
    class Meta:
        verbose_name = 'zoekvraag'
        verbose_name_plural = 'zoekvragen'

Daniel Roseman

unread,
Nov 20, 2015, 3:56:57 AM11/20/15
to Django users
And as I stated, you have no field "zoekvraag_id" there. The automatic PK field that Django creates is called "id" and can also be referenced as "pk". You need to use that in your {% url %} tag. There is no "logic in translation of class names".
--
DR.

Bob Aalsma

unread,
Nov 20, 2015, 10:45:52 AM11/20/15
to Django users
Cheers Daniel, I'll review, modify and try again!

Bob Aalsma

unread,
Nov 20, 2015, 1:51:20 PM11/20/15
to Django users
How big can a smile be?

Works! 
Thanks!

Bob
Reply all
Reply to author
Forward
0 new messages