[Answer]-NoReverseMatch at /<url>/

1👍

View parameters and the query string are not the same thing. The args and kwargs in the reverse function are the arguments that get passed to your view function. The query string, that is the parameters after the ? in your url, is parsed into the request.GET dictionary.

A simple solution would be to just append your query string to the url:

return HttpResponseRedirect('%s?event_id=%s' % (reverse('corporate_contribution'), event_id))

Another way, which is the method that is frequently used, is to capture part of the url and use it as an argument to your view:

urls.py:

url(r^corporate_contribution/(?P<event_id>[\d]+)/$, 'corporate_contribution', name='corporate_contribution')

views.py:

def corporate_contribution(request, event_id):
    ...

def other_view(request, *args, **kwargs):
    ...
    return HttpResponseRedirect(reverse('corporate_contribution',args=(event_id,)))

This will ensure that your view always gets an event_id passed. Note that the argument passed to the reverse function is the name of the url (defined by name='...'), not the actual url itself, so it definitely should not contain a slash. This is so that you can change the url itself without breaking your code, as the url will be changed everywhere.

👤knbk

Leave a comment