Directory
First, define the view
Second, the response
First, define the view
views.py
From django.shortcuts import renderfrom django.shortcuts import httpresponsefrom django.shortcuts import redirect# define Def login as a function (request): if request.method == "GET": return render ( request, "login.html") elif request.method == "POST": user = request. Post.get ("user") pwd = request. Post.get ("pwd") if user == "admin" and pwd == "123": return redirect ("/index/") else: return render (request, "login.html") else: # PUT,DELETE,HEAD... return redirect ("/index/") # class-Defined From django.views import viewclass home (View): The # parent will use the dispatch to get the request information and judge it, then execute the relevant method, return the data through the dispatch method, before, After you can do some custom actions before you perform the method def dispatch (Self, request, *args, **kwargs): # calling dispatch in the parent class print ("Before") result = super (Home, self). Dispatch (Request, *args, **kwargs) print ( "After") return result def get (self, request): print (Request.method) return render (request, "home.html") def post (self, request): print (Request.method) return render (request, "home.html")
Second, the response
1. Return string
Return HttpResponse ("string")
2. Return to HTML
return render (Request, "HTML template path", {data:data})
3. Redirection
return redirect ("/url Path")
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1927645
Python Road 60-django View