The view function Django

A. Views.py create files in the root directory of the project folder view files created views.py, in fact, any file name can use views to follow tradition.

Note: All views need to request a function object as the first parameter , request information on behalf of a string of client browser requests to the server, the function views the need to receive the string job to him for processing.

 

Two .HttpResponse function:

In views.py inside, we can use the page to return to the HttpResponse direct a string.

HttpResponse call directly to the page returns a string.

 

from django.http import HttpResponse

def hello(request):    

return HttpResponse("Hello World!")

 

three. The function returns a call to render the page:

render the meaning in English is provided:

It is mainly used to render a function that returns a html template page.

render(request,template_name,context=None,content_type=None,status=None,

using=None)

The effect of this method are: receiving a request, a binding template and a given context given context dictionary and returns after a HttpResponse object rendering. Popular talk is a request is received, and then specify a good html template, and the content context context dictionary, loaded into templates_name specified in the template file, and the browser rendering rendering.

Detailed parameters:

request: is a fixed parameter, refers to the number of requests received by wsgi processed by the client browser

according to.

template_name: HTML file templates defined, pay attention to the path, such as 'templates \ polls \ index.html', parameters should write 'polls \ index.html'

Guess you like

Origin www.cnblogs.com/hewanli/p/11724439.html