django 2.0-将应用程序中的urls文件包含到根urls文件中 [英] django 2.0 - including urls file from an app to the root urls file

查看:42
本文介绍了django 2.0-将应用程序中的urls文件包含到根urls文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对每个应用都有自己的urls.py文件感兴趣,然后使用诸如 include('myapp.urls')这样的include django函数将它们包含在根urls.py文件中.

这里的所有内容都遵循django教程:

我想念什么?

有一个变种可以使用,在这种情况下,我只需要将应用程序导入到 root urls.py ,如下所示:

django.url导入路径中的

 从作品集导入视图中urlspatterns = [path('',views.home,name ='home')] 

解决方案

页面错误非常明显.给定您的根URL '/' www.example.com/,在您的任何视图中均未找到url模式.由于您的网址仅包含 admin/ folio/.没有注册"/".

但是根据您的解决方法,您必须将URL映射到特定视图.

在您的情况下,将映射 folio.views.home .您可能需要创建一个主视图,以将 base.html 呈现为主页.

urls.py

从django.url导入的

 包含路径从main.views导入主页#main是一个呈现索引或主页的主应用程序.urlpatterns = [path('',home,name ="index"),路径('folio/',include('folio.urls'))] 

app_name/views.py

  def home(request):返回render(request,'main/base.html',{}) 

我还建议在folio.urls.py中在 urlpatterns 之前添加一个应用程序命名空间,并添加此 app_name ='folio',以便您可以导航到folio应用程序在您的 main/templates/main/base.html 模板中使用此url语法.

main/templates/main/base.html

 < a href ="{%url'folio:home'%}">转到folio主页</a> 

I'm interested in have for each app their own urls.py file and then, include them in the root urls.py file using the include django function like this include('myapp.urls').

Everything here follows the django tutorial: enter link description here

So, in root urls.py it is like this:

from django.url import include, path
urlpatterns = [ path('folio/', include('folio.urls')) ]

and now in the folio urls.py it is like the following:

from django.urls import path
from . import views
urlpatterns = [ path('', views.home, name='home') ]

After some tryings all I get is page not found.

What am I missing?

There's a variation to this to work, in this case I just need to import the app to the root urls.py as following:

from django.url import path
from folio import views
urlspatterns = [ path('', views.home, name='home') ]

解决方案

The page error is very clear. No urls pattern is found in any of your views given your root url '/' or www.example.com/. Since your urls only include admin/ and folio/. No '/' was registered.

But according to your workaround yes you must MAP A URL TO A PARTICULAR VIEW.

And in your case the folio.views.home is mapped. You might need to create a home view to render base.html as homepage.

urls.py

from django.url import include, path
from main.views import home # main is a master app that renders index or home page.

urlpatterns = [
    path('', home, name="index"),
    path('folio/', include('folio.urls'))
]

app_name/views.py

 def home(request):
     return render(request, 'main/base.html', {})

I also encourage todo an app namespacing, in your folio.urls.py before the urlpatterns add this app_name = 'folio' So you can navigate to your folio app home using this url syntax in your main/templates/main/base.html template.

main/templates/main/base.html

 <a href="{% url 'folio:home' %}">Go to folio home page</a>

这篇关于django 2.0-将应用程序中的urls文件包含到根urls文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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