PYTHON framework-DJANGO Study Notes (4)

Source: Internet
Author: User

PYTHON framework-DJANGO Study Notes (4)
Just like our first goal, it is very easy to create a webpage to output the famous example: Hello world. The first view Hello world. These are complete functions and import declarations. Input them to the views. py file: 1 from django. http import HttpResponse2 3 def hello (request): 4 return HttpResponse ("Hello World! \ NIt's my First django web! ") Here we mainly talk about: a view is a function of Python. The first parameter type of this function is HttpRequest; it returns an HttpResponse instance. To make a Python function a recognizable view of Django, it must meet these two conditions. Now, if you run python manage. py runserver again, you will also see the welcome page of Django, rather than the Hello world page we just wrote. That's because our mysite project does not know anything about the hello view. We need to explicitly tell it through a detailed URL and activate this view. (Continue with the example of publishing a static HTML file just now. Now we have created an HTML file, but we have not uploaded it to the server directory .) URLconf is used to bind view functions and URLs. URLconf is like a directory supported by Django. It is essentially a ing table between the URL mode and the view functions to be called for this URL mode. In this way, you tell Django that this code is called for this URL and that code is called for that URL. For example, when a user accesses/foo/, the view function foo_view () is called. This view function exists in the view. py file of the Python module. When you execute the django-admin.py startproject in the previous chapter, the script automatically creates a URLconf (that is, the urls. py file) for you ). The default URL. py will look like the following: Copy code 1 from django. conf. urls import patterns, include, url 2 3 from django. contrib import admin 4 admin. autodiscover () 5 6 urlpatterns = patterns ('', 7 # Examples: 8 # url (R' ^ $ ', 'myfirstsite. views. home ', name = 'home'), 9 # url (R' ^ blog/', include ('blog. urls '), 10 11 url (R' ^ admin/', include (admin. site. urls), 12) copy the code and Let's explain the code line by line: 1st lines import django. conf. urls. all modules under ults, which are Django URLs The basic structure of conf. This includes a patterns function that calls the patterns () function in line 11th and saves the returned results to the urlpatterns variable. Note the urlpatterns variable. Django expects to find it from the ROOT_URLCONF module. This variable defines the URL and the ing between codes used to process these URLs. By default, all contents of URLconf are commented out-the Django application is still in the white version. To add URL and view to URLconf, you only need to add Python tuple for ing URL mode and view function. this example shows how to add the "hello" function in the view. 1 from django. conf. urls. defaults import * 2 from mysite. views import hello3 4 urlpatterns = patterns ('', 5 ('^ hello/$', hello), 6) You need to make two changes here. First, the hello view is introduced in the module (mysite/views. py is translated into mysite. views in the import Syntax of Python. Next, we add a line for urlpatterns: ('^ hello/$', hello). This line is called URLpattern, which is a Python tuples. The first element in the tuples is the pattern matching string (Regular Expression), and the second element is the view function that will be used in that mode. Start the Django development server to test the modified URLconf and run the command line python manage. py runserver. (If you keep it running, the development server will automatically monitor code changes and automatically reload them, so you do not need to manually restart them.) the address of the Development Server is http: // 127.0.0.1: 8000/, open your browser to access http: // 127.0.0.1: 8000/hello /. You can see the output result. The development Server automatically detects Python code changes for necessary reload, so you do not need to restart the Server after the code changes. The server running address is ''http: // 127.0.0.1: 8000/''. Open your browser and enter ''http: // 127.0.0.1: 8000/hello /'', you will see the string output by your 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.