Python 使用时传递视图参数';模板中的url
在views.py中,我有以下内容:Python 使用时传递视图参数';模板中的url,python,django,Python,Django,在views.py中,我有以下内容: def logout(request,key): auth.logout(request) return HttpResponseRedirect(reverse('airAgency.views.index',args=[key])) 在index模板中,当用户单击链接时,注销视图运行: <a href="{% url airAgency.views.logout %}">logout</a> 假设agn.WebS
def logout(request,key):
auth.logout(request)
return HttpResponseRedirect(reverse('airAgency.views.index',args=[key]))
在index
模板中,当用户单击链接时,注销视图运行:
<a href="{% url airAgency.views.logout %}">logout</a>
假设agn.WebSite的值为mastane
以上代码导致此错误:
Caught NoReverseMatch while rendering: Reverse for 'airAgency.views.logout' with arguments '(u'mastane',)' and keyword arguments '{}' not found.
正确的方法是什么?捕获NoReverseMatch-通常意味着url被不正确的参数调用,或者{%url%}尝试调用指定的url时出现其他错误 您需要为要在url.py中调用的视图函数定义url模式。并在url模式中指定一个视图函数 url.py
url(r'^logout/(?P<key>[a-zA-Z0-9\-]+)/$', "airAgency.views.logout", name="logout"),
url(r'^logout/(?P[a-zA-Z0-9\-]+)/$,“airageneration.views.logout”,name=“logout”),
index.html
<a href="{% url logout agn.WebSite %}">
<a href="{% url logout agn.WebSite %}">