1、默认urls在项目文件夹下的urls.py 2、可以添加额外的urls.py文件,如下即将app1下的urls添加:
urlpatterns = [
...
path('',include('app1.urls')),
]
3、需要在urls.py中注册,规则 如:
path('index/', views.index),
4、需要在app的views.py中添加 index(request) 如下
from django.shortcuts import render
def index(request):
return render(request,'index.html') #index.html在该app下的templates目录
5、常见的响应 HttpResponse 四种方式:
- HttpResponse(): 响应 多种数据类型
return http.HttpResponse(content="响应体数据",content_type="响应体数据类型",status="状态码[default:200]")
- JsonResponse(): 响应 JSON数据
return redirect("/index/")
- redirect(): 重定向
data = {
"id":1111,
"name":'zhuge'
}
return http.JsonResponse(data)
- render(): 渲染响应HTML模板
return render(request,'index.html',{'data1':'','data2':''})
more:https://blog.csdn.net/qq_38582906/article/details/106203005

评论