13

I am trying to pass a parameter to my view, but I keep getting this error:

NoReverseMatch at /pay/how

Reverse for 'pay_summary' with arguments '(False,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pay/summary/$']

/pay/how is the current view that I'm at. (that is the current template that that view is returning).

urls.py

url(r'^pay/summary/$', views.pay_summary, name='pay_summary')

views.py

def pay_summary(req, option):
    if option:
        #do something
    else:
        #do something else
    ....

template

<a href="{% url 'pay_summary' False %}">my link</a>

EDIT

I want the view should accept a POST request, not GET.

Saša Kalaba
  • 4,241
  • 6
  • 29
  • 52
  • 2
    It's not clear what you want the url tag to return. Do you want the `False` to be part of the url e.g. `/pay/summary/False/` or a GET parameter e.g. `/pay/summary/?value=False`? – Alasdair Sep 14 '15 at 14:53
  • https://docs.djangoproject.com/en/1.8/topics/http/urls/ – Gocht Sep 14 '15 at 15:03
  • You are missing 2 things: 1. Parameter in the url definition, 2. The parameter needs to be an integer or a string, but not python boolean type(because url is a string). – Shang Wang Sep 14 '15 at 15:06

2 Answers2

26

To add to the accepted answer, in Django 2.0 the url syntax has changed:

path('<int:key_id>/', views.myview, name='myname')

Or with regular expressions:

re_path(r'^(?P<key_id>[0-9])/$', views.myview, name='myname')
Rexcirus
  • 2,459
  • 3
  • 22
  • 42
23

You need to define a variable on the url. For example:

url(r'^pay/summary/(?P<value>\d+)/$', views.pay_summary, name='pay_summary')),

In this case you would be able to call pay/summary/0

It could be a string true/false by replacing \d+ to \s+, but you would need to interpret the string, which is not the best.

You can then use:

<a href="{% url 'pay_summary' value=0 %}">my link</a>
Dric512
  • 3,525
  • 1
  • 20
  • 27
  • Ty that worked. Btw is there a way to do this without the /0 showing in the url? – Saša Kalaba Sep 14 '15 at 15:10
  • 1
    You have three ways to pass arguments: 1) Like in the answer, 2) with GET parameters (pay/summary?value=0), with POST parameters (Using forms). Only form with no show the value on the URL, but are not useful for links. – Dric512 Sep 14 '15 at 15:18
  • I think it should be a capital 'S' rather than lower case for a string – KDEx Apr 11 '16 at 01:06
  • I did not use Django since a very long time. Do you have by chance a reference in the doc so that I can update the answer? Otherwise I will just update to tell it is outdated. – Dric512 Oct 17 '18 at 05:41