1、创建第二个app
假设我们项目P下面已经有了一个默认的app,名字是app1。现在我想创建第二个app,名字时app2。
进入pychram下的Terminal中,运行命令:python manage.py startapp app2
此外,我在每个app下都建立一个urls.py,方便区分。
右击app名,点击“new”,选择“Python File”,命名为urls,确定。
2、设置url.py
(1)设置P\urls.py
引入app2下的urls,然后添加引导路径。具体代码如下:from app2 import urls as app2_urls
urlpatterns = [
path('app2/',include(app2_urls)), # 将所有以app2开头的urls,都交给app2(app名)下的urls.py处理
]
(2)设置app2\urls.pyfrom django.urls import path
from app2 import views
urlpatterns = [
path('index/', views.index),
]
3、编写app2下的views.pyfrom django.shortcuts import render,HttpResponse
# Create your views here.
# 绑定博客
def index(request):
return HttpResponse(111)
4、html页面如何调用url
一般在href或者src中调用一般这样写:href="/app2/index/"
这样就OK了。