[英]How to include urls with app_name without having to use reverse with app name in Django?
I have a custom directory for allauth
templates.我有一个用于
allauth
模板的自定义目录。 I need some custom urlpatterns
as well as I don't want some default views.我需要一些自定义
urlpatterns
以及我不想要一些默认视图。 I wanted to start with glueing in the entire allauth
and change things that I need later on.我想从粘合整个
allauth
开始,然后更改我以后需要的东西。
My main app config/urls.py
file我的主应用
config/urls.py
文件
from django.urls import include, path
urlpatterns = [
path("account/", include("users.urls")),
]
Users app:用户应用:
users/urls.py
app_name = "users"
urlpatterns = [
path("login/", allauth_views.login, name="account_login"),
path("logout/", allauth_views.logout, name="account_logout"),
path("signup/", allauth_views.signup, name="account_signup"),
]
With this basic setup, if I go to /account/login
it routes correctly to the desired view, but it is defined in users
app with the app_name = "users"
, therefore, I have to access it in other views with reverse(users:account_login)
.有了这个基本设置,如果我 go 到
/account/login
它会正确路由到所需的视图,但它是在users
应用程序中定义的app_name = "users"
,因此,我必须在其他视图中使用reverse(users:account_login)
。 The problem is that allauth
LoginView
has this method问题是
allauth
LoginView
有这个方法
def get_context_data(self, **kwargs):
ret = super(LoginView, self).get_context_data(**kwargs)
signup_url = passthrough_next_redirect_url(self.request,
reverse("account_signup"),
self.redirect_field_name)
redirect_field_value = get_request_param(self.request,
self.redirect_field_name)
site = get_current_site(self.request)
ret.update({"signup_url": signup_url,
"site": site,
"redirect_field_name": self.redirect_field_name,
"redirect_field_value": redirect_field_value})
return ret
which does the lookup on reverse("account_signup")
but Django gets lost and throws django.urls.exceptions.NoReverseMatch
.它在
reverse("account_signup")
上进行查找,但 Django 丢失并抛出django.urls.exceptions.NoReverseMatch
。
Two solutions:两种解决方案:
allauth
urlpatterns in my main urls file it works correctly but I want to keep this file small and define allauth
urls in my users
app.allauth
urlpatterns 它可以正常工作,但我想保持这个文件很小并在我的users
应用程序中定义allauth
urls。app_name
in my users/urls.py
it works properly but I want it for other routes.users/urls.py
中定义app_name
它可以正常工作,但我希望它用于其他路线。 I consider adding app_name
to be a good practice.app_name
是一个好习惯。 I could have created another app for allauth
without an app name but then I'd have users stuff in two apps.allauth
创建另一个应用,但是我会在两个应用中包含用户的东西。 None of these satisfy me.这些都不能让我满意。
What I want:我想要的是:
Can I somehow include
them without a namespace?我可以在没有命名空间的情况下以某种方式
include
它们吗? I tried path("account/", include("users.urls", namespace=None))
without any luck.我尝试
path("account/", include("users.urls", namespace=None))
没有任何运气。
I wasn't thinking... I can keep my custom users
urls in users/urls.py
with an app name and create another urls file like users/allauth_urls.py
in users
app and things work correctly.我没想到......我可以将我的自定义
users
url 保存在users/urls.py
中并使用应用程序名称,并在users
应用程序中创建另一个 urls 文件,如users/allauth_urls.py
并且一切正常。
Then, with然后,与
path("account/", include("users.allauth_urls")),
Things work as exepcted.事情按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.