SlideShare a Scribd company logo
2
Most read
The	
  Django	
  Book	
  
Chapter	
  3:	
  Views	
  and	
  URLconfs	
  
	
  
Speaker:	
  Vincent	
  Chien	
  
Introduction	
  
•  Views:	
  The	
  main	
  lesson	
  here	
  is	
  this:	
  a	
  view	
  is	
  just	
  a	
  Python	
  
funcAon	
  that	
  takes	
  an	
  HCpRequest	
  as	
  its	
  first	
  parameter	
  and	
  
returns	
  an	
  instance	
  of	
  HCpResponse.	
  
	
  
	
  
	
  
•  URLConfs:	
  To	
  hook	
  a	
  view	
  funcAon	
  to	
  a	
  parAcular	
  URL	
  with	
  
Django,	
  use	
  a	
  URLconf.	
  
	
  
	
  
	
  
	
  
•  Loose	
  Coupling(鬆散耦合):	
  changes	
  made	
  to	
  one	
  of	
  the	
  
pieces	
  will	
  have	
  liCle	
  or	
  no	
  effect	
  on	
  the	
  other.	
  
view function, and the URL is specified in a URLconf. First, let’s write our “Hello world” view function.
4.1.1 Your First View
Within the mysite directory that django-admin.py startproject made in the last chapter, create an em
file called views.py. This Python module will contain our views for this chapter. Note that there’s nothing spe
about the name views.py – Django doesn’t care what the file is called, as you’ll see in a bit – but it’s a good ide
call it views.py as a convention, for the benefit of other developers reading your code.
Our “Hello world” view is simple. Here’s the entire function, plus import statements, which you should type into
views.py file:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
Let’s step through this code one line at a time:
• First, we import the class HttpResponse, which lives in the django.http module. We need to import
class because it’s used later in our code.
• Next, we define a function called hello – the view function.
Each view function takes at least one parameter, called request by convention. This is an object that
tains information about the current Web request that has triggered this view, and it’s an instance of the c
Django Book Documentation, Release 0.1
Let’s step through this code one line at a time:
• The first line imports all objects from the django.conf.urls.defaults module, which is Django’s
URLconf infrastructure. This includes a function called patterns.
• The second line calls the function patterns and saves the result into a variable called urlpatterns. The
patterns function gets passed only a single argument – the empty string. (The string can be used to supply a
common prefix for view functions, which we’ll cover in Chapter 8.)
The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module.
This variable defines the mapping between URLs and the code that handles those URLs. By default, as we can see,
the URLconf is empty – your Django application is a blank slate. (As a side note, that’s how Django knew to show
you the “Welcome to Django” page in the last chapter. If your URLconf is empty, Django assumes you just started a
new project and, hence, displays that message.)
To add a URL and view to the URLconf, just add a Python tuple mapping a URL pattern to the view function. Here’s
how to hook in our hello view:
from django.conf.urls.defaults import *
from mysite.views import hello
urlpatterns = patterns(’’,
(’^hello/$’, hello),
)
(Note that we’ve removed the commented-out code for brevity. You can choose to leave those lines in, if you’d like.)
We made two changes here:
• First, we imported the hello view from its module – mysite/views.py, which translates into
mysite.views in Python import syntax. (This assumes mysite/views.py is on your Python path; see
the sidebar for details.)
How	
  Django	
  Process	
  a	
  Request	
  
•  A	
  request	
  comes	
  in	
  to	
  /hello/.	
  
•  Django	
  determines	
  the	
  root	
  URLconf	
  by	
  looking	
  at	
  the	
  
ROOT_URLCONF	
  seTng.	
  
•  Django	
  looks	
  at	
  all	
  of	
  the	
  URLpaCerns	
  in	
  the	
  URLconf	
  for	
  the	
  first	
  
one	
  that	
  matches	
  /hello/.	
  
•  If	
  it	
  finds	
  a	
  match,	
  it	
  calls	
  the	
  associated	
  view	
  funcAon.	
  
•  The	
  view	
  funcAon	
  returns	
  an	
  HCpResponse.	
  
•  Django	
  converts	
  the	
  HCpResponse	
  to	
  the	
  proper	
  HTTP	
  response,	
  
which	
  results	
  in	
  a	
  Web	
  page.	
  
you the “Welcome to Django” page in the last chapter. If your URLconf is empty,
new project and, hence, displays that message.)
To add a URL and view to the URLconf, just add a Python tuple mapping a URL p
how to hook in our hello view:
from django.conf.urls.defaults import *
from mysite.views import hello
urlpatterns = patterns(’’,
(’^hello/$’, hello),
)
(Note that we’ve removed the commented-out code for brevity. You can choose to
We made two changes here:
• First, we imported the hello view from its module – mysite/v
mysite.views in Python import syntax. (This assumes mysite/view
the sidebar for details.)
• Next, we added the line (’^hello/$’, hello), to urlpatterns. T
tern. It’s a Python tuple in which the first element is a pattern-matching str
this in a bit) and the second element is the view function to use for that patte
In a nutshell, we just told Django that any request to the URL /hello/ shoul
function.
Your Python Path
Your Python path is the list of directories on your system where Python looks w
URLconf	
  feature	
  
•  URLconf	
  passed	
  view	
  funciton	
  as	
  an	
  object	
  without	
  calling	
  the	
  
funciton.	
  This	
  is	
  a	
  key	
  feature	
  of	
  Python(and	
  other	
  dynamic	
  
languages):	
  funcitons	
  are	
  first-­‐class	
  objects,	
  which	
  means	
  you	
  
can	
  pass	
  them	
  around	
  just	
  like	
  any	
  other	
  variables.	
  
hCp://swaywang.blogspot.tw/2012/01/first-­‐class-­‐funcAon.html	
  
Regular	
  Expressions(1/2)	
  
•  caret(	
  ^	
  ):	
  require	
  that	
  the	
  paCern	
  matches	
  the	
  start	
  of	
  the	
  
string.	
  
•  dollar	
  sign(	
  $	
  ):	
  require	
  that	
  the	
  paCern	
  matches	
  the	
  end	
  of	
  
the	
  string.	
  
Regular	
  Expressions(2/2)	
  
hCp://en.wikipedia.org/wiki/Regular_expression	
  
	
  
	
  
Django Book Documentation, Release 0.1
Symbol Matches
. (dot) Any single character
d Any single digit
[A-Z] Any character between A and Z (uppercase)
[a-z] Any character between a and z (lowercase)
[A-Za-z] Any character between a and z (case-insensitive)
+ One or more of the previous expression (e.g., d+ matches one or more digits)
[^/]+ One or more characters until (and not including) a forward slash
? Zero or one of the previous expression (e.g., d? matches zero or one digits)
* Zero or more of the previous expression (e.g., d* matches zero, one or more than one
digit)
{1,3} Between one and three (inclusive) of the previous expression (e.g., d{1,3} matches
one, two or three digits)
For more on regular expressions, see http://www.djangoproject.com/r/python/re-module/.
4.1.3 A Quick Note About 404 Errors
At this point, our URLconf defines only a single URLpattern: the one that handles requests to the URL /hello/.
What happens when you request a different URL?
To find out, try running the Django development server and visiting a page such as
http://127.0.0.1:8000/goodbye/ or http://127.0.0.1:8000/hello/subdirectory/,
or even http://127.0.0.1:8000/ (the site “root”). You should see a “Page not found” message (see Figure
regex	
  example	
  
•  '^hello/’	
  
/hello/foo	
  
/hello/bar	
  
	
  
•  ‘hello/$’	
  
/foo/bar/hello/	
  
	
  
•  ‘hello/‘	
  
/foo/hello/bar	
  
	
  
•  r'^Ame/plus/d+/$’	
  
r'^Ame/plus/d{1,2}/$’	
  
	
  
	
  
raw	
  string	
  
r'n’	
  !=	
  ‘n’	
  
Dynamic	
  URLs	
  
•  Query	
  string	
  parameter	
  is	
  a	
  method.	
  
•  But	
  Django’s	
  URLconf	
  system	
  encourages	
  preCy	
  URLs.	
  
•  r'^Ame/plus/d+/$'	
  
•  r'^Ame/plus/d{1,2}/$'	
  
Python’s usage of backslashes and the backslashes that are found in regular expressions, so it’s s
you use raw strings any time you’re defining a regular expression in Python. From now on, all
this book will be raw strings.
Now that we’ve designated a wildcard for the URL, we need a way of passing that wildcard dat
so that we can use a single view function for any arbitrary hour offset. We do this by placing p
data in the URLpattern that we want to save. In the case of our example, we want to save whatev
in the URL, so let’s put parentheses around the d{1,2}, like this:
(r’^time/plus/(d{1,2})/$’, hours_ahead),
If you’re familiar with regular expressions, you’ll be right at home here; we’re using parenthese
the matched text.
The final URLconf, including our previous two views, looks like this:
from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead
urlpatterns = patterns(’’,
(r’^hello/$’, hello),
(r’^time/$’, current_datetime),
(r’^time/plus/(d{1,2})/$’, hours_ahead),
)
With that taken care of, let’s write the hours_ahead view.
Coding Order
Dynamic	
  URLs	
  
Django Book Documentation, Release 0.1
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>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)
Let’s step through this code one line at a time:
• The view function, hours_ahead, takes two parameters: request and offset.
– request is an HttpRequest object, just as in hello and current_datetime. We’ll say it again:
each view always takes an HttpRequest object as its first parameter.
– offset is the string captured by the parentheses in the URLpattern. For example, if the requested
URL were /time/plus/3/, then offset would be the string ’3’. If the requested URL were
/time/plus/21/, then offset would be the string ’21’. Note that captured values will always
Debug	
  suggestion	
  
•  assert	
  False	
  
Below the “Request information” section, the “Settings” section lists all of the settings for this particular Django
installation. (We’ve already mentioned ROOT_URLCONF, and we’ll show you various Django settings through-
out the book. All the available settings are covered in detail in Appendix D.)
The Django error page is capable of displaying more information in certain special cases, such as the case of template
syntax errors. We’ll get to those later, when we discuss the Django template system. For now, uncomment the offset
= int(offset) lines to get the view function working properly again.
Are you the type of programmer who likes to debug with the help of carefully placed print statements? You can use
the Django error page to do so – just without the print statements. At any point in your view, temporarily insert an
assert False to trigger the error page. Then, you can view the local variables and state of the program. Here’s an
example, using the hours_ahead view:
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
assert False
html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)
Finally, it’s obvious that much of this information is sensitive – it exposes the innards of your Python code and Django
configuration – and it would be foolish to show this information on the public Internet. A malicious person could use
it to attempt to reverse-engineer your Web application and do nasty things. For that reason, the Django error page is
only displayed when your Django project is in debug mode. We’ll explain how to deactivate debug mode in Chapter
12. For now, just know that every Django project is in debug mode automatically when you start it. (Sound familiar?
The “Page not found” errors, described earlier in this chapter, work the same way.)
4.7 What’s next?
So far, we’ve been writing our view functions with HTML hard-coded directly in the Python code. We’ve done that to
keep things simple while we demonstrated core concepts, but in the real world, this is nearly always a bad idea.

More Related Content

What's hot (20)

PDF
JavaScript Functions
Colin DeCarlo
 
PDF
Introduction to Python decorators
rikbyte
 
PDF
PyCon NZ 2013 - Advanced Methods For Creating Decorators
Graham Dumpleton
 
PDF
Lambdas and Streams Master Class Part 2
José Paumard
 
PPTX
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Blue Elephant Consulting
 
PPTX
Python Homework Help
Programming Homework Help
 
PDF
Java SE 8 for Java EE developers
José Paumard
 
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
PDF
Free your lambdas
José Paumard
 
PPTX
Computer Science Homework Help
Programming Homework Help
 
PDF
OOP and FP - Become a Better Programmer
Mario Fusco
 
PPT
JavaScript Functions
Brian Moschel
 
PDF
The Django Book chapter 4 templates (supplement)
Vincent Chien
 
PDF
Lambda and Stream Master class - part 1
José Paumard
 
PPTX
Build Lightweight Web Module
Morgan Cheng
 
PPT
Swiss army knife Spring
Mario Fusco
 
DOC
Functions
zeeshan841
 
PPTX
Python Homework Help
Programming Homework Help
 
PDF
Java SE 8 for Java EE developers
José Paumard
 
PDF
From object oriented to functional domain modeling
Mario Fusco
 
JavaScript Functions
Colin DeCarlo
 
Introduction to Python decorators
rikbyte
 
PyCon NZ 2013 - Advanced Methods For Creating Decorators
Graham Dumpleton
 
Lambdas and Streams Master Class Part 2
José Paumard
 
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Blue Elephant Consulting
 
Python Homework Help
Programming Homework Help
 
Java SE 8 for Java EE developers
José Paumard
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
Free your lambdas
José Paumard
 
Computer Science Homework Help
Programming Homework Help
 
OOP and FP - Become a Better Programmer
Mario Fusco
 
JavaScript Functions
Brian Moschel
 
The Django Book chapter 4 templates (supplement)
Vincent Chien
 
Lambda and Stream Master class - part 1
José Paumard
 
Build Lightweight Web Module
Morgan Cheng
 
Swiss army knife Spring
Mario Fusco
 
Functions
zeeshan841
 
Python Homework Help
Programming Homework Help
 
Java SE 8 for Java EE developers
José Paumard
 
From object oriented to functional domain modeling
Mario Fusco
 

Viewers also liked (9)

PPTX
Django getting start
shengwu83
 
PPTX
Font End Development + Automation with Django
Evan Reiser
 
PDF
Class-based views with Django
Simon Willison
 
PPTX
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
PDF
Django class based views for beginners
Spin Lai
 
PDF
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Nina Zakharenko
 
PDF
Djangocon 2014 angular + django
Nina Zakharenko
 
PDF
Create responsive websites with Django, REST and AngularJS
Hannes Hapke
 
PDF
RESTful API Design, Second Edition
Apigee | Google Cloud
 
Django getting start
shengwu83
 
Font End Development + Automation with Django
Evan Reiser
 
Class-based views with Django
Simon Willison
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
Django class based views for beginners
Spin Lai
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Nina Zakharenko
 
Djangocon 2014 angular + django
Nina Zakharenko
 
Create responsive websites with Django, REST and AngularJS
Hannes Hapke
 
RESTful API Design, Second Edition
Apigee | Google Cloud
 
Ad

Similar to The Django Book / Chapter 3: Views and URLconfs (20)

PDF
Django Introduction & Tutorial
之宇 趙
 
PPTX
Django framework
TIB Academy
 
PDF
Views&urls
Camilla Ke
 
PPTX
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
salemsg
 
PPTX
Django - Python MVC Framework
Bala Kumar
 
PDF
Web Development with Python and Django
Michael Pirnat
 
PDF
Django tutorial
Ksd Che
 
DOCX
Akash rajguru project report sem v
Akash Rajguru
 
PDF
Chapter 8- Advanced Views and URLconfs
Vincent Chien
 
PDF
Django design-patterns
Agiliq Info Solutions India Pvt Ltd
 
PPT
DJango
Sunil OS
 
PDF
django_introduction20141030
Kevin Wu
 
PPTX
Web development with django - Basics Presentation
Shrinath Shenoy
 
PDF
Django - basics
University of Technology
 
PDF
Build and deploy Python Django project
Xiaoqi Zhao
 
KEY
Web application development with Django framework
flapiello
 
ODP
Web Development in Django
Lakshman Prasad
 
PPTX
Django Girls Tutorial
Kishimi Ibrahim Ishaq
 
PDF
The Django Web Application Framework
Simon Willison
 
Django Introduction & Tutorial
之宇 趙
 
Django framework
TIB Academy
 
Views&urls
Camilla Ke
 
WRStmlDSQUmUrZpQ0tFJ4Q_a36bc57fe1a24dd8bc5ba549736e406f_C2-Week2.pptx
salemsg
 
Django - Python MVC Framework
Bala Kumar
 
Web Development with Python and Django
Michael Pirnat
 
Django tutorial
Ksd Che
 
Akash rajguru project report sem v
Akash Rajguru
 
Chapter 8- Advanced Views and URLconfs
Vincent Chien
 
Django design-patterns
Agiliq Info Solutions India Pvt Ltd
 
DJango
Sunil OS
 
django_introduction20141030
Kevin Wu
 
Web development with django - Basics Presentation
Shrinath Shenoy
 
Django - basics
University of Technology
 
Build and deploy Python Django project
Xiaoqi Zhao
 
Web application development with Django framework
flapiello
 
Web Development in Django
Lakshman Prasad
 
Django Girls Tutorial
Kishimi Ibrahim Ishaq
 
The Django Web Application Framework
Simon Willison
 
Ad

Recently uploaded (20)

PPTX
PYLORIC STENOSIS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
FULL DOCUMENT: Read the full Deloitte and Touche audit report on the National...
Kweku Zurek
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
PPTX
Folding Off Hours in Gantt View in Odoo 18.2
Celine George
 
PPTX
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PDF
07.15.2025 - Managing Your Members Using a Membership Portal.pdf
TechSoup
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
Blanket Order in Odoo 17 Purchase App - Odoo Slides
Celine George
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PPTX
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PYLORIC STENOSIS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
FULL DOCUMENT: Read the full Deloitte and Touche audit report on the National...
Kweku Zurek
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Folding Off Hours in Gantt View in Odoo 18.2
Celine George
 
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
07.15.2025 - Managing Your Members Using a Membership Portal.pdf
TechSoup
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Blanket Order in Odoo 17 Purchase App - Odoo Slides
Celine George
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 

The Django Book / Chapter 3: Views and URLconfs

  • 1. The  Django  Book   Chapter  3:  Views  and  URLconfs     Speaker:  Vincent  Chien  
  • 2. Introduction   •  Views:  The  main  lesson  here  is  this:  a  view  is  just  a  Python   funcAon  that  takes  an  HCpRequest  as  its  first  parameter  and   returns  an  instance  of  HCpResponse.         •  URLConfs:  To  hook  a  view  funcAon  to  a  parAcular  URL  with   Django,  use  a  URLconf.           •  Loose  Coupling(鬆散耦合):  changes  made  to  one  of  the   pieces  will  have  liCle  or  no  effect  on  the  other.   view function, and the URL is specified in a URLconf. First, let’s write our “Hello world” view function. 4.1.1 Your First View Within the mysite directory that django-admin.py startproject made in the last chapter, create an em file called views.py. This Python module will contain our views for this chapter. Note that there’s nothing spe about the name views.py – Django doesn’t care what the file is called, as you’ll see in a bit – but it’s a good ide call it views.py as a convention, for the benefit of other developers reading your code. Our “Hello world” view is simple. Here’s the entire function, plus import statements, which you should type into views.py file: from django.http import HttpResponse def hello(request): return HttpResponse("Hello world") Let’s step through this code one line at a time: • First, we import the class HttpResponse, which lives in the django.http module. We need to import class because it’s used later in our code. • Next, we define a function called hello – the view function. Each view function takes at least one parameter, called request by convention. This is an object that tains information about the current Web request that has triggered this view, and it’s an instance of the c Django Book Documentation, Release 0.1 Let’s step through this code one line at a time: • The first line imports all objects from the django.conf.urls.defaults module, which is Django’s URLconf infrastructure. This includes a function called patterns. • The second line calls the function patterns and saves the result into a variable called urlpatterns. The patterns function gets passed only a single argument – the empty string. (The string can be used to supply a common prefix for view functions, which we’ll cover in Chapter 8.) The main thing to note here is the variable urlpatterns, which Django expects to find in your URLconf module. This variable defines the mapping between URLs and the code that handles those URLs. By default, as we can see, the URLconf is empty – your Django application is a blank slate. (As a side note, that’s how Django knew to show you the “Welcome to Django” page in the last chapter. If your URLconf is empty, Django assumes you just started a new project and, hence, displays that message.) To add a URL and view to the URLconf, just add a Python tuple mapping a URL pattern to the view function. Here’s how to hook in our hello view: from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns(’’, (’^hello/$’, hello), ) (Note that we’ve removed the commented-out code for brevity. You can choose to leave those lines in, if you’d like.) We made two changes here: • First, we imported the hello view from its module – mysite/views.py, which translates into mysite.views in Python import syntax. (This assumes mysite/views.py is on your Python path; see the sidebar for details.)
  • 3. How  Django  Process  a  Request   •  A  request  comes  in  to  /hello/.   •  Django  determines  the  root  URLconf  by  looking  at  the   ROOT_URLCONF  seTng.   •  Django  looks  at  all  of  the  URLpaCerns  in  the  URLconf  for  the  first   one  that  matches  /hello/.   •  If  it  finds  a  match,  it  calls  the  associated  view  funcAon.   •  The  view  funcAon  returns  an  HCpResponse.   •  Django  converts  the  HCpResponse  to  the  proper  HTTP  response,   which  results  in  a  Web  page.   you the “Welcome to Django” page in the last chapter. If your URLconf is empty, new project and, hence, displays that message.) To add a URL and view to the URLconf, just add a Python tuple mapping a URL p how to hook in our hello view: from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns(’’, (’^hello/$’, hello), ) (Note that we’ve removed the commented-out code for brevity. You can choose to We made two changes here: • First, we imported the hello view from its module – mysite/v mysite.views in Python import syntax. (This assumes mysite/view the sidebar for details.) • Next, we added the line (’^hello/$’, hello), to urlpatterns. T tern. It’s a Python tuple in which the first element is a pattern-matching str this in a bit) and the second element is the view function to use for that patte In a nutshell, we just told Django that any request to the URL /hello/ shoul function. Your Python Path Your Python path is the list of directories on your system where Python looks w
  • 4. URLconf  feature   •  URLconf  passed  view  funciton  as  an  object  without  calling  the   funciton.  This  is  a  key  feature  of  Python(and  other  dynamic   languages):  funcitons  are  first-­‐class  objects,  which  means  you   can  pass  them  around  just  like  any  other  variables.   hCp://swaywang.blogspot.tw/2012/01/first-­‐class-­‐funcAon.html  
  • 5. Regular  Expressions(1/2)   •  caret(  ^  ):  require  that  the  paCern  matches  the  start  of  the   string.   •  dollar  sign(  $  ):  require  that  the  paCern  matches  the  end  of   the  string.  
  • 6. Regular  Expressions(2/2)   hCp://en.wikipedia.org/wiki/Regular_expression       Django Book Documentation, Release 0.1 Symbol Matches . (dot) Any single character d Any single digit [A-Z] Any character between A and Z (uppercase) [a-z] Any character between a and z (lowercase) [A-Za-z] Any character between a and z (case-insensitive) + One or more of the previous expression (e.g., d+ matches one or more digits) [^/]+ One or more characters until (and not including) a forward slash ? Zero or one of the previous expression (e.g., d? matches zero or one digits) * Zero or more of the previous expression (e.g., d* matches zero, one or more than one digit) {1,3} Between one and three (inclusive) of the previous expression (e.g., d{1,3} matches one, two or three digits) For more on regular expressions, see http://www.djangoproject.com/r/python/re-module/. 4.1.3 A Quick Note About 404 Errors At this point, our URLconf defines only a single URLpattern: the one that handles requests to the URL /hello/. What happens when you request a different URL? To find out, try running the Django development server and visiting a page such as http://127.0.0.1:8000/goodbye/ or http://127.0.0.1:8000/hello/subdirectory/, or even http://127.0.0.1:8000/ (the site “root”). You should see a “Page not found” message (see Figure
  • 7. regex  example   •  '^hello/’   /hello/foo   /hello/bar     •  ‘hello/$’   /foo/bar/hello/     •  ‘hello/‘   /foo/hello/bar     •  r'^Ame/plus/d+/$’   r'^Ame/plus/d{1,2}/$’      
  • 8. raw  string   r'n’  !=  ‘n’  
  • 9. Dynamic  URLs   •  Query  string  parameter  is  a  method.   •  But  Django’s  URLconf  system  encourages  preCy  URLs.   •  r'^Ame/plus/d+/$'   •  r'^Ame/plus/d{1,2}/$'   Python’s usage of backslashes and the backslashes that are found in regular expressions, so it’s s you use raw strings any time you’re defining a regular expression in Python. From now on, all this book will be raw strings. Now that we’ve designated a wildcard for the URL, we need a way of passing that wildcard dat so that we can use a single view function for any arbitrary hour offset. We do this by placing p data in the URLpattern that we want to save. In the case of our example, we want to save whatev in the URL, so let’s put parentheses around the d{1,2}, like this: (r’^time/plus/(d{1,2})/$’, hours_ahead), If you’re familiar with regular expressions, you’ll be right at home here; we’re using parenthese the matched text. The final URLconf, including our previous two views, looks like this: from django.conf.urls.defaults import * from mysite.views import hello, current_datetime, hours_ahead urlpatterns = patterns(’’, (r’^hello/$’, hello), (r’^time/$’, current_datetime), (r’^time/plus/(d{1,2})/$’, hours_ahead), ) With that taken care of, let’s write the hours_ahead view. Coding Order
  • 10. Dynamic  URLs   Django Book Documentation, Release 0.1 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>In %s hour(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html) Let’s step through this code one line at a time: • The view function, hours_ahead, takes two parameters: request and offset. – request is an HttpRequest object, just as in hello and current_datetime. We’ll say it again: each view always takes an HttpRequest object as its first parameter. – offset is the string captured by the parentheses in the URLpattern. For example, if the requested URL were /time/plus/3/, then offset would be the string ’3’. If the requested URL were /time/plus/21/, then offset would be the string ’21’. Note that captured values will always
  • 11. Debug  suggestion   •  assert  False   Below the “Request information” section, the “Settings” section lists all of the settings for this particular Django installation. (We’ve already mentioned ROOT_URLCONF, and we’ll show you various Django settings through- out the book. All the available settings are covered in detail in Appendix D.) The Django error page is capable of displaying more information in certain special cases, such as the case of template syntax errors. We’ll get to those later, when we discuss the Django template system. For now, uncomment the offset = int(offset) lines to get the view function working properly again. Are you the type of programmer who likes to debug with the help of carefully placed print statements? You can use the Django error page to do so – just without the print statements. At any point in your view, temporarily insert an assert False to trigger the error page. Then, you can view the local variables and state of the program. Here’s an example, using the hours_ahead view: def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) assert False html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html) Finally, it’s obvious that much of this information is sensitive – it exposes the innards of your Python code and Django configuration – and it would be foolish to show this information on the public Internet. A malicious person could use it to attempt to reverse-engineer your Web application and do nasty things. For that reason, the Django error page is only displayed when your Django project is in debug mode. We’ll explain how to deactivate debug mode in Chapter 12. For now, just know that every Django project is in debug mode automatically when you start it. (Sound familiar? The “Page not found” errors, described earlier in this chapter, work the same way.) 4.7 What’s next? So far, we’ve been writing our view functions with HTML hard-coded directly in the Python code. We’ve done that to keep things simple while we demonstrated core concepts, but in the real world, this is nearly always a bad idea.