当同一项目中有多个应用程序时,如何在Django中设置URL? [英] How to set up urls in django when there are multiple apps within the same project?

查看:52
本文介绍了当同一项目中有多个应用程序时,如何在Django中设置URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新手.我正在尝试设置URL.我在"mysite"项目下有2个应用.在mysite的urls.py中,我包括了两个应用程序(称为登录"和组织")的网址

I am new to django. I am trying to setup the URLs. I have 2 apps under a project 'mysite'. In the urls.py for mysite, I have included the urls from the 2 apps (called 'login' and 'org')

mysite/urls.py

mysite/urls.py

urlpatterns = [
    url(r'^login/', include('login.urls')),
    url(r'^org/', include('org.urls')),
]

login/urls.py

login/urls.py

app_name = 'login'
urlpatterns = [
    url(r'^signup/$', login_views.signup, name='signup'),
]

org/urls.py

org/urls.py

app_name = 'org'
urlpatterns = [
    url(r'^organizations/add/$', views.addorg, name='addorg'),
]

以这种方式进行设置后,我的网站网址变为:

Having set it up this way, my site URLs become the following:

site.com/login/login/signup
site.com/org/organizations/add

我希望这些URL如下所示.

I would like the URLs so that they are like the following.

site.com/login/signup
site.com/organizations/add

如何使包含"起作用?如果将mysite/urls.py更改为以下内容,它会起作用吗?

How do I get the 'includes' to work? Will it work if I change mysite/urls.py to the following?

urlpatterns = [
    url(r'^$', include('login.urls')),
    url(r'^$', include('org.urls')),
]

当有多个应用程序时,是否有更好的方法来设置URL?

Is there a better way of setting up the URLs when there are multiple apps?

谢谢

推荐答案

在您描述的设置中,登录网址不是'site.com/login/login/signup',而是'site.com/login/signup'.第二个'login/'来自何处?

In the setup you describe, the login url would not be 'site.com/login/login/signup', but 'site.com/login/signup'. Where would the second 'login/' come from?

您建议的解决方案

urlpatterns = [
    url(r'^$', include('login.urls')),
    url(r'^$', include('org.urls')),
]

如果两个应用程序中都没有相同的网址,则

通常可以使用.但这并不理想,因为将对所有请求不必要地尝试登录urs中的所有模式.

will generally work if there is no url that occurs identically in both apps. But it is not ideal, since all the patterns in the login urs will be unnecessarily tried for all requests.

因此,您可能要考虑删除 specific_app.urls 内部前缀,特别是如果这些前缀对于应用程序的所有视图而言都是相同的.将其留给将要使用并包含该应用程序的用户(此处为:mysite.urls),他们希望将其包含在该路径下.

Thus, you might want to consider removing the specific_app.urls internal prefixes, especially if those prefixes are the same for all views of the app. Leave it to whoever will use and include that app (here: mysite.urls) under which path they want to include the app.

# login/urls.py
urlpatterns = [
    # no 'login' prefix here. Includers know which app 
    # they are including and how they want do that.
    url(r'^signup/$', login_views.signup, name='signup'),  
]

# org/urls.py
urlpatterns = [
    # same: no 'org' prefix
    url(r'^add/$', views.addorg, name='addorg'),
]

# mysite.urls.py
urlpatterns = [
    url(r'^login/$', include('login.urls')),
    url(r'^organizations/$', include('org.urls')),
]

结果网址为:

site.com/login/signup
site.com/organizations/add

这篇关于当同一项目中有多个应用程序时,如何在Django中设置URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆