带有两个应用程序的urls.py中的Django Include() [英] Django Include() in urls.py with two apps

查看:44
本文介绍了带有两个应用程序的urls.py中的Django Include()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是一个简单的问题,但是我很难弄清楚为什么它不起作用.

I believe this is a simple question but I am having a hard time figuring out why this is not working.

我有一个django项目,并且添加了第二个应用程序(销售).在第二个应用程序之前,我的urls.py只需将以下内容路由到第一个应用程序(图表)即可:

I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:

urlpatterns = [

    path('admin/', admin.site.urls),
    path('', include('chart.urls')),
]

效果很好.

我已经看过许多教程,一遍又一遍地阅读了文档,所以我的印象是,我可以简单地将urls.py修改为:

I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:

urlpatterns = [

    path('admin/', admin.site.urls),
    path('sales/', include('sales.urls')),
    path('', include('chart.urls')),
]

,它首先应查找带有sales/的URL,如果找到该URL,则应将其路由到sales.urls,如果找不到,则继续并将其路由到chart.urls.但是,当我加载此代码并键入127.0.0.1:8000/sales时,它将返回以下错误:

and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/sales/
Raised by:  chart.views.index

这告诉我,它不是将我的sales/url路由到sales.urls,而是路由到chart.urls.当我将 path('',include('chart.urls'))更改为 path('',include('sales.urls'))时,它确实将其路由到sales.urls,因此我知道我的sales.urls文件设置正确.

which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.

我知道这可能是一个简单的问题,但我无法用已阅读的内容弄清楚.感谢您的帮助.

I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.

chart.urls:

chart.urls:

from django.urls import path, re_path
from . import views

urlpatterns = [
    path('dashboard/', views.chart, name='dashboard'),
    path('', views.index, name='index', kwargs={'pagename': ''}),
    path('<str:pagename>/', views.index, name='index'),
]

sales.urls:

sales.urls:

from django.urls import path
from . import views

urlpatterns = [
    path('sales/', views.sales, name='Sales Dashboard'),
]

推荐答案

在您的情况下,我认为销售页面的正确网址应为 http://127.0.0.1:8000/sales/sales/.

I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.

这是因为 sales/然后正在查找包含的 sales.urls ,而不在根/处找到任何url(唯一其中包含的网址为 sales/(在 sales/内),因此将为 sales/sales/

This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/

回答一个老问题,但对其他人可能有用...

这篇关于带有两个应用程序的urls.py中的Django Include()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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