Django_book third chapter


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

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/

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 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 in the same directory of the file server, load the configuration information

2) Which specifies the ROOT_URLCONF told the Django the same directory

3) When a user access, Django according to the parameters of loading, 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

from django.http import HttpResponse
import datetime

def current_datetime(request):
        now =
        html = "<html><body>It's now %s" % now
        return HttpResponse(html)

2) Edit

from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime

urlpatterns = patterns('',
        #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, add the following

from django.http import Http404, HttpResponse
import datetime

def hours_ahead(request, offset): 
                offset = int(offset)
        except ValueError:
                raise Http404()

        dt = + datetime.timedelta(hours=offset)
        html = "<html><body>It's %s hours, it will be %s</body></html>" % (offset, dt)
        return HttpResponse(html)

2) Edit, as follows

from django.conf.urls.defaults import *
from mysite.views import hello,current_datetime,hours_ahead

urlpatterns = patterns('',

#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