Django-05- view function

http request objects generated in two core:

        http request: HttpRequest objects

        http response: HttpResponse objects

Location: django.http

Before we used parameter request is HttpRequest detection method: isinstance (request, HttpRequest)

1. HttpRequest Properties and Methods

# Path: the full path to the page request does not include the domain name 
# 
# Method: The method of HTTP request string used in FIG. ALL CAPS representation. For example 
# 
# IF req.method == "GET": 
# 
# do_something () 
# 
# ELSEIF req.method == "POST": 
# 
# do_something_else () 
# 
# GET: HTTP GET contains all the parameters of dictionary-like object 
# 
# POST: dictionary-like object containing all the HTTP POST parameters 
# 
# server receives an empty case of a POST request also may occur, that is to say, the form form by 
submitting a request # HTTP POST method, but may not have the form data, and therefore can not use 
# if req.POST to determine whether to use the HTTP POST method; you should use req.method == iF "POST" 
# 
# 
#
# COOKIES: The cookies contain all the standard Python dictionary object; keys and values are strings. 
# 
# The FILES: dictionary-like object containing all uploaded files; a Key is in the FILES each <input type = "file" name = "" /> value of the name attribute Tags, FILES Each value is also a standard python dictionary object that contains the following three Keys: 
# 
# filename: Upload file name, represented by a string 
# content_type: content Type file upload 
original content uploaded file: content # 
# 
# 
# the User: is a django.contrib .auth.models.User object that represents the user currently logged in. If a visiting user is currently 
# no login, user will be initialized instance django.contrib.auth.models.AnonymousUser is. You 
# can pass user's is_authenticated () method to identify whether the user login: 
# IF req.user.is_authenticated (); only activated in Django AuthenticationMiddleware 
when the property is available # 
#
# Session: the only read-write property, on behalf of the current session of the dictionary object; they have activated session support Django in the property is available. 

# Method 
get_full_path (), for example: http: //127.0.0.1: 8000 / index33 / name = 123, result req.get_full_path () get is / index33 / name = 123?? 
Req.path: / index33

2. HttpResponse Properties and Methods

For HttpRequest object, it is automatically created by the django, however, HttpResponse objects have to create our own. Each view must return a request processing method HttpResponse object.

HttpResponse类在django.http.HttpResponse

HttpResponse object extending over conventional methods:

Page rendering: render () (recommended) <br> render_to_response (), 
page jump: redirect ( "path") 
about locals (): can directly function in all the variables passed to the template
def show_time(request):
    # return HttpResponse("hello")
    t = time.ctime()
    name = 'lsf'
    # return render(request, 'index.html', {"t": t, 'name':name})
    # return render_to_response('index.html', {"t": t, 'name':name})
    return render(request, 'index.html', locals())
def register(request):
    if request.method == "POST":
        print(request.POST.get('user'))
        print(request.POST.get('age'))
        # return render(request, 'login.html')
        return redirect('/login/')
    return render(request, 'register.html')


def login(request):
    return render(request, 'login.html')

render and redirect the difference between:

  • render the page template language to render the need to load data into the database needed to html, then all of this part in addition to writing the login view function, you must also write in the register, the code duplication, no decoupling.
  • do not jump to the url / login /, but still / register /, so you have to log in when the refresh again after.

 

Guess you like

Origin www.cnblogs.com/lsf123456/p/11308227.html