django视图层
用户打开网页访问,请求先到 url路由(urls.py)查看是否有对应的地址,
根据url path 访问 对应的请求
urls.py 的信息
from django.contrib import admin from django.urls import path,re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), re_path('index',views.index), re_path('login', views.login), ]
views.py的信息
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def index(request): print(request.GET) print(request.GET.get("name")) print(request.POST) print(request.path) print(request.get_full_path) return HttpResponse("INDEX") def login(request): if request.method =="POST": user = request.POST.get("user") pwd = request.POST.get("pwd") if user == "alex" and pwd =="123": return redirect("/index/") return render(request,"login.html")
常见的请求方式分为2种: get post
request.GET request.POST request.path # /index/ request.get_full_path # /index/?s=y HttpResponse() render(request,"login.html" {"ctime":ctime}) redirect("/index/")