Django:带有超链接的URL命名空间

时间:2015-08-15 23:31:54

标签: python django django-urls

我有网址:

url(r'^accounts/', include('allauth.urls'))

我创建了一个超链接注册,我想将用户定向到"/accounts/signup/"。当我没有放入命名空间时,我得到一个错误,当我将url:{% url accounts:account_signup%}作为超链接时,命名空间“accounts”不存在。当我放入namespace="accounts"时,如果我只是尝试通过在网址栏中输入网址来尝试访问网址,则会出现NoReverseMatch错误。

为什么我无法仅为上面的网址(name="accounts")命名,而allauths中的网址名称为"account_signup",只是将{% url accounts:account_signup%}作为链接?我不明白为什么使用include当我在应用程序中的任何地方不使用重复名称时,我被迫拥有命名空间。另外,我不明白为什么会出现NoReverseMatch错误。

1 个答案:

答案 0 :(得分:0)

你应该尝试像这样

在您的项目urls.py中,您应该将名称空间提供为

 url(r'^accounts/', include('allauth.urls',namespace="accounts"))

在你的项目的app urls.py中,你应该为使用这种语法调用url的网址提供“name”参数

action = {% url accounts:account_signup%}  //this is the action attribute of form tag

the url should be 

url(r'^account_signup/$',views.methodname,name='account_signup')

通过在url中为这种类型的url添加Name参数,你将不会得到“没有反向匹配发现错误”

或者您可以直接在html的表单标记中为您要重定向的页面提供网址,如下所述

action="/accounts/create_estimate/"   //this is the action attribute of form tag

您无需提供Name参数即可直接提供表单标记

的url in action属性