8. URLs and Views¶
Now you can start to build the frontend for the marcador app. In this chapter two URLs and their corresponding views will be created. The first is a simple list view of all public bookmarks, the second is a list of all bookmarks from a particular person.
8.1. Configure the URLs¶
“A clean, elegant URL scheme is an important detail in a high-quality Web application. Django lets you design URLs however you want, with no framework limitations.”
So that the views can be used, paths must be associated with them. In Django, this is done explicitly with a URL configration (URLconf). We’ll create a new URLconf just for the marcador app, so that later on the project will have a cleaner structure.
8.1.1. Add URLs to the application¶
Create a new file named urls.py
in the directory
mysite/marcador
and paste in the following lines:
1 2 3 4 5 6 7 8 | from django.conf.urls import url
urlpatterns = [
url(r'^user/(?P<username>[-\w]+)/$', 'marcador.views.bookmark_user',
name='marcador_bookmark_user'),
url(r'^$', 'marcador.views.bookmark_list', name='marcador_bookmark_list'),
]
|
Every URLconf must define a variable urlpatterns
which is a list of URLs.
Each URL will be created with the url()
function. In our example url()
is given three parameters: the path, the names of the views, and an identifier
(name), with which you can refer to this URL later. In the documentation you’ll
find a list of further parameters.
With Django the paths are defined as Regular Expressions (regexes). Regexes are a powerful tool with which to describe strings of characters by means of syntactic rules. You can find a detailed introduction to this theme at regular-expressions.info. The Online regex tester and debugger can help you to craft regular expressions for new URLs.
The regex for the list view, r'^$'
, consists of the control
characters ^
(the beginning of the string) and $
(the end of the
string). When nothing is in-between these two characters, they describe
an empty string. That means that the view is reachable from the path
/
, the home page.
The regex for the view with the bookmarks of a particular user is a bit
more complex: r'^user/(?P<username>[-\w]+)/$'
. It contains a
variable part that enables the user names to be filtered out. So with
/user/alice/
the bookmarks from Alice can be requested, and with
/user/bob/
, the bookmarks from Bob.
The regexes are composed as follows: after the control character for the
beginning ^
comes user/
, a static part that must match exactly.
Then follows a grouping (?P<username>[-\w]+)
. This means that all
the following letters, numbers, underscores and dashes (defined by
[-\w]+
) can be accessed using the variable username
. Django
makes sure that all groupings are passed to the view as arguments. At
the end is the static part /
and the control character for the end
of the string $
. With Django it’s conventional for all paths to end
with a /
.
8.1.2. Add URLs to the project¶
So that the URLconf for our app can be found, it must be referenced in the root
URLconf. So paste the emphasized line into the file
mysite/mysite/urls.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | """mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('marcador.urls')),
]
|
8.1.3. Test the new URLs¶
Now you can visit both URLs in your browser:
For both URLs a ViewDoesNotExist
exception will be raised. This
means that the URLs were correctly resolved, but the associated views do
not exist.
8.2. Add the views¶
“A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response.”
The next step is to add the views. In Django a view is a function that receives a request object and returns a response object. The function contains the logic that determines the content of the response.
8.2.1. A view to display all bookmarks¶
The first view gives a list of the public bookmarks. Open the file
views.py
in the directory mysite/marcador/
and paste the
following code in:
1 2 3 4 5 6 7 8 9 | from django.shortcuts import get_object_or_404, redirect, render
from .models import Bookmark
def bookmark_list(request):
bookmarks = Bookmark.public.all()
context = {'bookmarks': bookmarks}
return render(request, 'marcador/bookmark_list.html', context)
|
First of all the database query for all public bookmarks is be generated
by means of the ORM, and assigned to the variable bookmarks
. This is
then in turn stored in the dictionary context
. All keys in this
dictionary will later be available to the templates as variables.
Finally the function render()
generates the response object from the
request, the path to the template and the dictionary. render()
is a
shortcut, that carries out several steps in one. You’ll need the
shortcuts get_object_or_404()
and redirect()
for the following
views. You can find out what exactly the shortcuts do in the
documentation.
8.2.2. A view for each user¶
The second view lists all public bookmarks from a particular user. The new rows that you must paste in are highlighted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, redirect, render
from .models import Bookmark
def bookmark_list(request):
bookmarks = Bookmark.public.all()
context = {'bookmarks': bookmarks}
return render(request, 'marcador/bookmark_list.html', context)
def bookmark_user(request, username):
user = get_object_or_404(User, username=username)
if request.user == user:
bookmarks = user.bookmarks.all()
else:
bookmarks = Bookmark.public.filter(owner__username=username)
context = {'bookmarks': bookmarks, 'owner': user}
return render(request, 'marcador/bookmark_user.html', context)
|
It has an additional argument username
, with which the requested user in
the database is sought. This contains the same variable as in the regex
of the URLconf. If the user is not found, the HTTP status code
404 will be returned automatically. Whether the user exists and whether he or
she is the logged-in user is checked with the help of the relationship, defined by the field Bookmark.owner
with the model
User
, and the bookmarks loaded. If the view for another user is invoked,
the public bookmarks are filtered with the help of the argument username
.
Bookmarks and user are then added to the context dictionary, so that they can
be accessed in the template later.
8.2.3. Test the new views¶
You can now test both URLs:
Of course both currently produce a TemplateDoesNotExist
exception, because
the templates do not yet exist. Notice that the second URL will only work if
you have an user named “admin”. Otherwise use the username of the superuser you
created when you ran the createsuperuser command.