尝试将上下文从模板传递到我的视图(无论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/$']
知道问题是什么吗?
答案 0 :(得分:2)
改变路线:
url(r'^(?P<ad>\w+)$', views.post, name='post'),
更好的回答:
url(r'^(?P<ad>(True|False))$', views.post, name='post'),