from django.urls import path #引用HTTP协议的代码 from django.shortcuts import HttpResponse def yimi(request): #request参数保存了全部和用户浏览器请求相关的数据,返回指定内容 return HttpResponse(r'hello world') urlpatterns = [ path('admin/', admin.site.urls), #yimi/是浏览器的路径,yimi是函数 path('yimi/',yimi), ]
一、使用HttpResponse返回html
def xiaohei(request): with open("./templates/xiao.html","r",encoding="utf-8") as f: data =f.read() return HttpResponse(data)
二、使用render返回django
from django.shortcuts import render def xiaohei(request): return render(request,"xiao.html")
三、返回一些提示内容浏览器
3.1在HTML里加入 error 是标识符函数
<p>{{ error }}</p>
3.2使用render 标识符要一直url
def xiaohei(request): msg = '错误' return render(request,"zzz.html",{"error":msg})
from django.shortcuts import redirect def xiaohei(request): return redirect("http://www.baidu.com")
from 应用名 import views urlpatterns = [ path('yimi/',views.yimi) ]