在博文:
https://blog.csdn.net/wenhao_ir/article/details/131166889
中,已经讲述了怎么创建项目和应用,这里直接给出命令:
CD E:\Python_project\P_001\myshop-test
E:
django-admin startproject learn001
CD E:\Python_project\P_001\myshop-test\learn001
python manage.py startapp app0
python manage.py startapp app1
python manage.py startapp app2
找开文件:\learn001\urls.py
E:\Python_project\P_001\myshop-test\learn001\learn001\urls.py
把原代码:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
改为:
from django.urls import path, include
from app0 import views
urlpatterns = [
path('index/', views.index),
path('', include('app1.urls')),
path('', include('app2.urls')),
]
在路径:
E:\Python_project\P_001\myshop-test\learn001\app1
下新建文件 urls.py
然后写入下面的内容:
from django.urls import path
from app1 import views
urlpatterns = [
path('app1/index/', views.index),
]
在路径:
E:\Python_project\P_001\myshop-test\learn001\app2
下新建文件 urls.py
然后写入下面的内容:
from django.urls import path
from app2 import views
urlpatterns = [
path('app2/index/', views.index),
]
三个views.py 文件的内容分别如下:
E:\Python_project\P_001\myshop-test\learn001\app0\views.py
from django.shortcuts import render # 默认导入的模块
from django.http import HttpResponse # 导入HttpResponse模块
# Create your views here.
def index(request):
return render(request, 'index.html') # 将渲染结果输出到index.html模板中
E:\Python_project\P_001\myshop-test\learn001\app1\views.py
from django.shortcuts import render # 默认导入的模块
from django.http import HttpResponse # 导入HttpResponse模块
# Create your views here.
def index(request):
return render(request, 'index-app1.html') # 将渲染结果输出到index-app1.html模板中
E:\Python_project\P_001\myshop-test\learn001\app2\views.py
from django.shortcuts import render # 默认导入的模块
from django.http import HttpResponse # 导入HttpResponse模块
# Create your views here.
def index(request):
return render(request, 'index-app2.html') # 将渲染结果输出到index-app2.html模板中
按博文:https://blog.csdn.net/wenhao_ir/article/details/131166889中的“07-2 注册模板目录”进行注册。
在 index.html 中写入下面的内容:
<div style="color:red;font-size:24px;">这里是index.html模板文件div>
在 index-app1.html 中写入下面的内容:
<div style="color:blue;font-size:24px;">这里是 index-app1.html模板文件div>
在 index-app2.html 中写入下面的内容:
<div style="color:green;font-size:24px;">这里是 index-app2.html模板文件div>
CD E:\Python_project\P_001\myshop-test\learn001
python manage.py runserver 127.0.0.1:8012
访问:
http://127.0.0.1:8012/index/
访问:
http://127.0.0.1:8012/app1/index/
http://127.0.0.1:8012/app2/index/
https://pan.baidu.com/s/1nZALn6qiYI7g3UDeJ5VYPg?pwd=hnwi