自学内容网 自学内容网

Django-应用及分布式路由

什么是应用?

创建应用:

 

配置文件:

分布式路由:

 

分布式路由配置:

 

主路由配置:

from django.contrib import admin
from django.urls import path,include,re_path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    # 主路由链接需要加/
    path('music/',include("music.urls")),
    path('sport/',include("sport.urls")),
    path('news/',include("news.urls"))
]

app下配置(以news为例):

urls:

from django.urls import path
from . import views



urlpatterns = [
    path('index', views.news_view, name='index')
]

 views:

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.

def news_view(request):
    return render(request, 'news/index.html')

 此时就会有以下问题:

文件同名导致查询的html错误

解决方法:

可以在app下的templates目录下再创建一个同名目录,然后将html文件放到该目录下 


原文地址:https://blog.csdn.net/weixin_67852201/article/details/142961863

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!