Python Road 60-django View

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.