Create the first static view in Django, django View
As our first goal is to create a webpage to output this famous example:
Hello world.
If you have published a Hello World page, but you have never used a webpage frame, simply enter Hello World in the hello.html text file and upload it to any web server. Note that in this process you have already explained two key information about this web page: it includes (string "Hello world") and its URL (http://www.example.com/hello.html, if you put the file in a subdirectory, it may also be a http://www.example.com/files/hello.html ).
Using Django, you will use different methods to describe that the content of these two pages is generated by view function, and the URL is defined in URLconf. First, write a Hello World View function.
Create an empty file named views. py in the mysite folder created using the django-admin.py startproject in the previous chapter. This Python module will contain the view of this chapter. Note that Django does not have special requirements on the file name of view. py. It does not care what this file is called. However, according to the conventions, it is a good idea to name it view. py, which helps other developers understand your code, just as you can easily read this article.
Our Hello world view is very simple. These are complete functions and import declarations. You need to enter the views. py file:
From django. http import HttpResponsedef hello (request): return HttpResponse ("Hello world ")
We will analyze this code line by line:
- First, we import the HttpResponse class from the django. http module. See Appendix H for details