1. As our first goal, let's create a Web page to output this famous example message: Hello world. If you publish a simple Hello World Web page without a Web framework, simply type Hello world into a text file, name it a hello.html file, and upload it to a directory on a Web server. Note that in this process you have specified two of the most important information about the page: its contents (the string "Hello World") and its URL (for example, http://www.example.com/hello.html).
2. In the case of Django, you can specify both things, but in a different way. The content of the page is generated by a view function, and the URL is specified in urlconf. First, let's write the Hello world view function.
In the first view of the internal MySite directory that we created in the previous chapter, we created an empty file named views.py. This Python module will contain a view of this chapter. Be sure to place the file in the internal MySite directory, which is the mysite\mysite\ directory, not the directory that contains the manage.py. Our Hello world view is simple. Here is the entire function, plus the import statement, which you should enter into the views.py file.
from Import HttpResponse def Hello (Request): return HttpResponse ("HelloWorld"
Let's complete this line of code one step at a time: first, we import the class class, which is in the Django HTTP module. We need to import this class because it is later used in our code.
Next, we define a function called the Hello view function. Each view function requires at least one parameter, which is called a request by convention. This is a message that contains the current Web request that triggered the view, and is also a class django.http. An instance of HTTP.
In this case, we don't use the request to do anything, but it must be the first parameter of the view. Note that the name of the view function is not important; To make Django aware of this problem, it doesn't need to be named in some way.
We call it hello here, because the name clearly shows the gist of the idea, but it can also be named Hello_wonderful_beautiful_world, or the same disgusting thing.
The next section, your first urlconf, will explain how Django found this feature. This function is a simple one-line program: it simply returns a HttpResponse object that has been instantiated with the text "Hello World". The main lesson here is that the view is just a Python function that takes a parameter as its first argument and returns an instance of HttpResponse.
In order for the Python function to be a Django view, it must do both things. (There are exceptions, but we'll talk about it later.)
At this point, your first urlconf needs to run the Python manage.py runserver command again, and you can still see the Django message, without any change in our Hello world view. This is because our MySite project does not yet know the Hello view; we need to explicitly tell Django that we have activated this view on a specific URL.
To continue our previous analogy of publishing a static HTML file, we have now created the HTML file, but did not upload it to a directory on the server.
In order to connect the view function with the specific URL of Django, we used a urlconf.
Urlconf is similar to the content table of your Django-driven Web site. Basically, it is the map between the URL and the view function that should call those URLs. This is what you tell Django, because this URL, call this code, and call the code for that URL. For example, when someone accesses url/foo/, the View function Fooview () is called, which is located in the Python module views.py.
When Django-admin Startproject was executed in the previous chapter, the script automatically created a urlconf: file urls.py for you.
By default, this is the case
"""mysite URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.11/topics/http/urls/examples:function views 1. ADD an import:from My_app import views 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, Name= ' home ") class-based views 1. ADD an import:from other_app.views import Home 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') including another URLconf 1. Import the Include () function:from django.conf.urls import URL, include 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (' Blog.urls '))""" fromDjango.conf.urlsImportURL fromDjango.contribImportAdminurlpatterns=[url (r'^admin/', Admin.site.urls),]
If we omit the document comment at the top of the file, this is the essence of urlconf
from Import URL from Import = [ url (r'^admin/', Admin.site.urls),]
Let's complete this code one step at a time: The first line imports two functions from the Django.conf.urls module. Includes a URL that allows you to include a full Python import path to another urlconf module, and a regular expression that matches a URL in the browser to a module in a Django project. The second line is called from the django.contrib模块中
function Admin. This function is called by the include function to load the URL of the Django administration site.
The third line is urlpatterns
a simple list of URL () instances.
The main problem to note here is that the variable Urlpatterns,django want to find it in your urlconf module. This variable defines the mapping between the URL and the code that handles the URLs. To add URLs and views to urlconf, simply add a mapping between the URL pattern and the view function. Here's how to connect in the Hello view
from django.conf.urls import URL from django.contrib import admin from mysite.views import hellourlpatterns = [url (r '
We made two changes here: First, we import the Hello View from its module mysite/views.py, which translates to MySite. The view in the Python import syntax. (This assumes that the mysite/views.py is on your Python path.) Next, we added a line URL (r ' hello/$ ', hello) to Urlpatterns. This line is called Urlpattern. This URL () function tells Django how to handle the URL you are configuring. The first parameter is a pattern-matching string (a regular expression; discussed in more detail) The second argument is the view function used for the pattern. The URL () can also select other optional parameters, which we will discuss in more depth in the 7th chapter.
One of the more important details we introduce here is the R character in front of the regular expression string.
This tells Python that the string is an original string and its contents should not be interpreted as backslashes.
In a normal Python string, a backslash is used to escape a special character in the string n, and the string n is a character string that contains a line break.
When you add R as an original string, Python does not apply its backslash, so R ' \ n ' is a two-character string that contains a text backslash and a lowercase n.
There is a natural conflict between the backslash in Python and the backslash found in the regular expression, so it is best to use the original string when you redefine the Django regular expression.
To put it simply, we just told Django that any request for url/hello/should be handled by the Hello view function.
It is worth discussing this urlpattern syntax, as it may not be so obvious.
Although we want to match url/hello/, the pattern looks a little different.
This is why
Django removes the slash from each input URL before checking the URL pattern.
This means that our urlpattern does not include the main slash in the/hello/.
At first, this may seem less intuitive, but this requirement simplifies the inclusion of URLCONFS in other Urlconfs, which we'll cover in chapter 7th.
The pattern consists of a caret (^) and a dollar sign ($).
These are regular expression characters with special meanings: caret (^) means that the pattern matches the beginning of the string, while the dollar sign indicates that the pattern matches the end of the string.
This concept is best explained by example, if we are using pattern hello/(at the end there is no dollar symbol), if we had instead used the pattern ^hello/(without a dollar sign at the end), th En any URL starting with/hello/would match, such as/hello/foo And/hello/bar, not just/hello/.
Similarly similarly, if we had left off retains the initial caret character (i.e., hello/$), Django would match matches any URL that ends With hello/, such as/foo/bar/hello/. If we had simply used hello/, without a caret or dollar sign, then any URL containing contains hello/would match, such as/foo/ Hello/bar. Thus, so we use both the caret and dollar sign to ensure make sure that's the url/hello/matches nothing more, nothing l Less ESS.
Basics of Django Creating dynamic Web pages