传递上下文以从模板中查看:NoReverseMatch错误

时间:2017-07-21 08:58:24

标签: python django

尝试将上下文从模板传递到我的视图(无论ad = True还是False)。以下是我尝试过的方法:

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'),

模板

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

post.views

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

模板中的ad='True'应传递到views并将默认ad=False更改为ad=True。相反,我收到此错误消息:

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

知道问题是什么吗?

1 个答案:

答案 0 :(得分:2)

改变路线:

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

更好的回答:

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