Let us pause to learn a little more about how Django works. Specifically, when you view your Hello World message by visiting http://127.0.0.1:8000/hello/ in your web browser, what does Django do behind the scenes? It all starts with the settings file.
When you run:
$ python manage.py runserver
the script looks for a file called settings.py in the inner myD3site directory. This file contains all sorts of configuration for this particular Django project, all in uppercase:
TEMPLATE_DIRS
DATABASES
.. and so on.
The most important setting is called ROOT_URLCONF. ROOT_URLCONF tells Django which Python module should be used as the URLConf for this website.
Remember when django-admin startproject created the files settings.py and urls.py? Open the settings.py file and see for yourself; it should look like this:
ROOT_URLCONF = 'myD3site.urls'
This corresponds to the file myD3site/urls.py. When a request comes in for a particular URL - say, a request for /hello/ - Django loads the URLconf pointed to by the ROOT_URLCONF setting. Then it checks each of the URLpatterns in that URLconf, in order, comparing to the requested URL with the patterns one at a time, until it finds one that matches.
When it finds one that matches, it calls the view function associated with that pattern, passing it an HttpRequest object as the first parameter. (We will cover the specifics of HttpRequest later. ) As we saw in our first view example, a view function must return an HttpResponse.
Once it does this, Django does the rest, converting the Python object to a proper web response with the appropriate HTTP headers and body (that is, the content of the web page).
In summary:
You now know the basics of how to make Django-powered pages. It is quite simple, really just write view functions and map them to URLs via URLconfs.
When you run:
$ python manage.py runserver
the script looks for a file called settings.py in the inner myD3site directory. This file contains all sorts of configuration for this particular Django project, all in uppercase:
TEMPLATE_DIRS
DATABASES
.. and so on.
The most important setting is called ROOT_URLCONF. ROOT_URLCONF tells Django which Python module should be used as the URLConf for this website.
Remember when django-admin startproject created the files settings.py and urls.py? Open the settings.py file and see for yourself; it should look like this:
ROOT_URLCONF = 'myD3site.urls'
This corresponds to the file myD3site/urls.py. When a request comes in for a particular URL - say, a request for /hello/ - Django loads the URLconf pointed to by the ROOT_URLCONF setting. Then it checks each of the URLpatterns in that URLconf, in order, comparing to the requested URL with the patterns one at a time, until it finds one that matches.
When it finds one that matches, it calls the view function associated with that pattern, passing it an HttpRequest object as the first parameter. (We will cover the specifics of HttpRequest later. ) As we saw in our first view example, a view function must return an HttpResponse.
Once it does this, Django does the rest, converting the Python object to a proper web response with the appropriate HTTP headers and body (that is, the content of the web page).
In summary:
- A request comes in to /hello/.
- Django determines the root URLconf by looking at the ROOT_URLCONF setting.
- Django looks at all of the URL patterns in the URLconf for the first one that matches /hello/.
- If it finds a match, it calls the associated view function.
- The view function returns an HttpResponse.
- Django converts the HttpResponse to the proper Http Response, which results in a webpage.
You now know the basics of how to make Django-powered pages. It is quite simple, really just write view functions and map them to URLs via URLconfs.
No comments:
Post a Comment