Create the first static view in Django

Source: Internet
Author: User
This article mainly introduces how to create the first static view in Django. Like other programming languages, Helloworld is used as an example, for more information, see create a webpage as our first goal 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

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.