This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

URL Routing and Creating our first view


By now you should have some working models that allow you to create some recipes by logging into the built in django admin. This week we will be learning about how django routes urls and creating our first view to make a web application come alive. 

URL Dispatcher

In your project directory you will notice a file called urls.py. We had to use it in order to enable the django admin, but we haven't created anything from scratch here. 

Let's create a path for a homepage that will be used to describe our awesome recipe website. Here's how we do that:

url(r'^$', 'recipe.views.homepage', name='homepage'),
 
What this means is that the url dispatcher will look for an exact match of essentially nothing, as in the root of your website (i.e http://www.recipizza.com/). It will not match anything past the trailing slash. We are also passing a view function 'recipe.views.homepage' where recipe is my app, views is the views file in the recipe folder, and homepage is the name of the function that will handle this url. We have named our url homepage which is something you should always do. We'll learn more about that when we jump into the template layer. 
 
The URL Dispatcher uses regular expressions to match words, letters, symbols, etc. This makes it extremely powerful for even complex rules. For the most part, this project won't need to do too much regex magic, but I think it is something any djangonaut should learn well, otherwise it will look like magic and it shouldn't. Take some time to read up on regular expressions. 
 
Creating our first view
Here's something I wish someone would have told me upfront when I was learning Django. It's Python! Your url patterns are functions that take arguments. Views are functions that return an HTTP Response. The url pattern is trying to match anything you type into the address bar and when it finds a match it passes it to the correct view function. If you know how to create a function then you know how to create url patterns and views. There's no magic; it's just Python. 
 
Open your views.py file in the recipe app and add the following:
 
# We will use direct_to_template which is actually a shortcut for returning an HttpResponse.
# You should read the django tutorial about this to learn what a view requires
# I use direct_to_template most often to return an http response as it's clean and clear
# However, before using it you should understand what it does
 
from django.views.generic.simple import direct_to_template
 
# Oh look it's just a function that takes request as an argument! Request is built in to django
# it contains information from the current http request, we'll talk more about it later
def homepage(request):
    # Return an http response that serves the index.html template
    return direct_to_template(request, 'homepage/index.html')
 
Fire up your dev server and go to http://localhost:8000/
You should get a nice error that it could not find homepage/index.html. That's a good thing! That means our url pattern found a match, called the view function 'homepage', but failed because it couldn't find the template specified in the function. 
 
Create a new folder in your project directory (not your app folder) called 'templates'
Create a folder called homepage and create a file called index.html
Write "hello world" in index.html and save it
Go to your settings.py file and add the templates folder you just created to the TEMPLATE_DIRS setting. 
 
TEMPLATE_DIRS = (
    'templates/'
)
 
Reload your dev server and you should now see 'Hello World'! 
 
This is the basic plumbing for django web applications. Not too hard right? Just remember that you are dealing with python functions. Django is doing the heavy lifting to make sure the right content get's served. Learn regular expressions, read up about http responses, and url patterns with regex. Finally read the documentation on direct_to_template. It's buried in there, but trust me it's one of the best shortcuts in django and a shame they don't put it in the official tutorial. 
 
Your Assignment:
  1. Create a url pattern for the homepage using my guide above
  2. Create a url pattern that matches '/about-us' and returns a page with some about us content

Extra credit:

Read about how to pass an argument to a view function using regular expressions and escaping. You will find it in the django documentation. 

 

Task Discussion