Django_book third chapter
Catalog
Three, try and URL configuration... 1
3.1 the first view (static content)... 1
The first 3.2 of the URLconf 1
3.3 about 2 error page...
3.4 the working flow of Django... 2
3.5 second views (dynamic content)... 2
3.6 third views (dynamic URL)... 3
Three, try and URL configuration
3.1 the first view (static content)
Create view files, command is not mandatory, but named view makes it easier to understand
$ cd mysite/ $ touch views.py
The contents are as follows
from django.http import HttpResponse def hello(request): return HttpResponse("hello world")
The first 3.2 of the URLconf
1) Although the create view file, but now access and can't see we set the hello page, because know nothing at all site sites to that view, we need a url told Django
The url is in the MySite/urls.py
from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns('', ('^hello/$',hello), #Here is a mapping between URL and views, all point to the request of /hello/ Hello will be the view function )
2) UrlpatternGrammar:
Before Django check the URL will begin to remove each application slash (/), ^ and $belongs to the regular expression content
3) Modification is complete without restarting the server, the server will automatically monitor the development of code changes and reload, refresh.
A 3.3 error page
1) If the visit is not defined in a url.py page, the page back more information, so do not easily exposed on the Internet, increase security hidden danger
3.4 the working flow of Django
1) When running runserver, in and manager.py in the same directory of the setting.py file server, load the configuration information
2) Which specifies the ROOT_URLCONF told the Django the same directory url.py
3) When a user access, Django according to the parameters of loading url.py, according to the tuple information in urlpattern to sequentially match
4) Match after a call to view in the view function, return a HttpResponse, returns the webpage content
3.5 second views (dynamic content)
1) Edit views.py
from django.http import HttpResponse import datetime def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It's now %s" % now return HttpResponse(html)
2) Edit url.py
from django.conf.urls.defaults import * from mysite.views import hello, current_datetime urlpatterns = patterns('', ('^hello/$',hello), ('^time/$',current_datetime), ('^another_time/$',current_datetime), #Another URL to call the current_datetime view )
3) Access to the time view
3.6 third views (dynamic URL)
Review just examples, although the page content is dynamic or static, but URL
1) Edit views.py, add the following
from django.http import Http404, HttpResponse import datetime def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>It's %s hours, it will be %s</body></html>" % (offset, dt) return HttpResponse(html)
2) Edit url.py, as follows
from django.conf.urls.defaults import * from mysite.views import hello,current_datetime,hours_ahead urlpatterns = patterns('', ('^hello/$',hello), ('^time/$',current_datetime), ('^another_time/$',current_datetime), (r'^time/plus/(\d{1,2})/$',hours_ahead), )
#R is to let Django do not deal with tuple in the backslash(\)
3) Access to different simulation of non fixed url address
This third chapter, to be continued~
Posted by Randall at November 12, 2013 - 1:33 PM