Zorgan
Zorgan

Reputation: 9153

Passing context to view from template: NoReverseMatch error

Trying to pass context from template to my view (whether ad=True or False). Here's how I've tried to do it:

urls.py

url(r'^$', home, name='bv'),
url(r'^q/', search, name='search'),
url(r'^post/', include('post.urls')),

post.urls

url(r'^$', views.post, name='post'),
url(r'^edit/(?P<id>\d+)/', views.edit, name='edit'),
url(r'^delete/(?P<id>\d+)/', views.delete, name='delete'),

template

<a href="{% url 'post' ad='True' %}">Proceed</a>

post.views

def post(request, ad=False):
    ...

the ad='True' in the template should pass onto the views and change the default ad=False to ad=True. Instead, I get this error message:

NoReverseMatch at /advertise/
Reverse for 'post' with arguments '()' and keyword arguments '{'ad': 'True'}' 
not found. 1 pattern(s) tried: ['post/$']

Any idea what the problem is?

Upvotes: 0

Views: 85

Answers (1)

Hadi Farhadi
Hadi Farhadi

Reputation: 1771

change route:

url(r'^(?P<ad>\w+)$', views.post, name='post'),

and better answer:

url(r'^(?P<ad>(True|False))$', views.post, name='post'),

Upvotes: 2

Related Questions