First Django-based page: Hello World
As our first goal, create a Web page that is used to output this famous example message: Hello world.
First view
The Hello world view is very simple. These are the complete functions and import declarations, which are entered into the views.py file:
1 from Import HttpResponse 2 3 def Hello (Request): 4 return HttpResponse ("Hello world!\nit ' s my first Django web! ")
The main point here is that a view is a function of Python. The type of the first parameter of this function is HttpRequest; it returns a HttpResponse instance. In order for a Python function to be a Django-aware view, it must satisfy both of these conditions.
The first of the urlconf
Now, if you run again: Python manage.py runserver, you'll also see the Django Welcome page without seeing the Hello World display page we just wrote. That's because our MySite project also knows nothing about the Hello view. We need to explicitly tell it and activate this view through a URL that is described in detail. (Continue with our example of publishing a static HTML file just now.) Now that we have created the HTML file, we have not uploaded it to the server directory. In order to bind the view functions and URLs, we use urlconf.
URLconf It's like a directory of Django-supported websites. Its essence is the URL pattern and the mapping table between the view functions to invoke for that URL pattern. That's how you tell Django to call that code for that URL and call that code for that URL. For example, when a user accesses/foo/, the View function Foo_view () is called, and this view function exists in the Python module file view.py.
when you execute django-admin.py startproject in the previous chapter , the script automatically builds a copy of your URLconf (that is, urls.py files). The default urls.py will look like this:
1 fromDjango.conf.urlsImportpatterns, include, url2 3 fromDjango.contribImportAdmin4 Admin.autodiscover ()5 6Urlpatterns = Patterns ("',7 #Examples:8 #URL (r ' ^$ ', ' myfirstsite.views.home ', name= ' home '),9 #URL (r ' ^blog/', include (' Blog.urls ')),Ten OneURL (r'^admin/', include (Admin.site.urls)), A)
Let's explain the code on a line-by-row basis:
It is now important to note that the urlpatterns variable, Django expects to be able to find it from the root_urlconf module. The variable defines the mapping between the URL and the code used to process the URLs. By default, URLconf all the content is commented out--django application or white version piece.
If you want to add URLs and view to urlconf, simply add the python tuple that maps the URL pattern and view functionality. Here's how to add the Hello feature in view.
1 from Import *2fromimport Hello34 urlpatterns = Patterns ( "' , 5 ('^hello/$', hello), 6 )
There are two changes that need to be made here.
First, from the module (in the import syntax of Python, mysite/views.py translates to mysite.views ), the Hello c11> view.
Next, we add a line for Urlpatterns: (' ^hello/$ ', hello), which is called the Urlpattern, which is a python tuple. The first element in a tuple is a pattern-matching string (a regular expression), and the second element is the view function that the pattern will use.
Start the Django Development server to test the modified URLconf, run the command line < Span class= "Pre" >python manage.py runserver . (If you keep it running, the development server automatically monitors code churn and automatically re-loads, so you don't need to restart manually) the address of the development server is Http://127.0.0.1:8000/ &NBSP;, open your browser to access http:// 127.0.0.1:8000/hello/ . You can see the output. The development server will automatically detect changes to Python code to do the necessary reloading, so you do not need to restart the server after the code changes. The server runs the address ' http://127.0.0.1:8000/', so open the browser and enter '
At this point, the first Django Web page is complete.
The Python framework's Django Learning Note (iv)