项目url 添加应用访问路径
from django.contrib import admin
from django.urls import path, include
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('app1/', include('app1.urls')), # 在主项目添加应用的所有路由路径
]
就可以访问app1应用下的url.py
from django.urls import path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
path('test', views.test, name='test'),
]
app1创建路由文件urls.py
from django.urls import path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
path('test', views.test, name='test'),
]
app1 views.py视图文件
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse("app1 的index")
def test(request):
return render(request, '1/index.html', {}) # 这里获取的路径文件为:templates/1/index.html