简体   繁体   English

如何在 Django 中包含带有 app_name 的 url,而不必使用带有应用名称的反向?

[英]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:两种解决方案:

  1. If I set those 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.如果我在我的主 urls 文件中设置这些allauth urlpatterns 它可以正常工作,但我想保持这个文件很小并在我的users应用程序中定义allauth urls。
  2. If I don't define 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.

相关问题 如何在不提供 app_name 的情况下测试 django 应用程序 - How to test django app without giving an app_name Django 2名称空间和app_name - Django 2 namespace and app_name Django social_django&#39;在include()中指定名称空间而不提供app_name&#39; - Django social_django 'Specifying a namespace in include() without providing an app_name ' 在没有app_name的情况下url.py中的include关键字不起作用(Django 2.0) - Include keyword in url.py not working without app_name(Django 2.0) 不支持在 include() 中指定命名空间而不提供 app_name - Specifying a namespace in include() without providing an app_name is not supported 在 include() 中指定命名空间而不提供 app_name。 给 app_name 也不起作用 - Specifying a namespace in include() without providing an app_name. Giving app_name also doesn't work 使用Django时出现SyntaxError app_name - SyntaxError app_name while using Django Django 2.0.7 URL app_name的工作方式 - Django 2.0.7 how URL app_name works 为什么 django-postman 不能用这个 -> url(r'^messages/', include('postman.urls', namespace='postman', app_name='postman')), - Why with django-postman that's doesn't work with this -> url(r'^messages/', include('postman.urls', namespace='postman', app_name='postman')), 未命名模块<app_name> - No module named <app_name>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM