Django Routing Sample
Open-source sample provided by AppSeed to explain Django routing mechanism.
This sample might be useful to beginners to understand how the routing system works in Django Web Framework. In the end, the sample will route three things: a default route that shows a simple Hello World, a 2nd route that displays a random number at each page refresh, and the last route that shows a random image pulled from the internet.
Resources
​Support (Email and LIVE on Discord) for registered AppSeed users.
​

What is Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Read more about Django Framework - quick introduction
​

How to code this sample

This sample can be coded from scratch by following the steps below.
Check Python Version
1
$ python --version
2
Python 3.8.4 <-- All good
Copied!
Create/activate a virtual environment - Unix-based system
1
$ # Virtualenv modules installation
2
$ virtualenv env
3
$ source env/bin/activate
Copied!
For Windows, the syntax is slightly different
1
$ # virtualenv env
2
$ # .\env\Scripts\activate
Copied!
Install Django
1
$ pip install django
Copied!
Create a new Django Project
1
$ mkdir django-sample-urls
2
$ cd django-sample-urls
Copied!
Inside the new directory, we will invoke startproject subcommand
1
$ django-admin startproject config .
Copied!
Note: Take into account that . at the end of the command.
Setup the database
1
$ python manage.py makemigrations
2
$ python manage.py migrate
Copied!
Start the app
1
$ python manage.py runserver
2
$
3
$ # Access the web app in browser: http://127.0.0.1:8000/
Copied!
At this point we should see the default Django page in the browser:
Django - Default Project Page.
Create a new Django app
1
$ python manage.py startapp sample
Copied!

​

Add a simple route

Let's edit sample/views.py as shown below:
1
def hello(request):
2
return HttpResponse("Hello Django")
Copied!
Configure Django to use the new route - update config/urls.py as below:
1
from django.contrib import admin
2
from django.urls import path
3
from django.conf.urls import include, url # <-- NEW
4
from sample.views import hello # <-- NEW
5
​
6
urlpatterns = [
7
path('admin/', admin.site.urls),
8
url('', hello), # <-- NEW
9
]
Copied!
In other words, the default route is served by hello method defined in sample/views.py.

​

New Route - Dynamic content

Let's create a new route that shows a random number - sample/views.py.
1
...
2
from random import random
3
...
4
def myrandom(request):
5
return HttpResponse("Random - " + str( random() ) )
Copied!
The new method invoke random() from Python core library, converts the result to a string and returns the result. The browser output should be similar to this:
Django Routing - Dynamic Content Route.
​

New Route - Random Images

This route will pull a random image from a public (and free) service and inject the returned content into the browser response. To achieve this goal, we need a new Python library called requests to pull the random image with ease.
The controller code should be defined in sample/views.py.
1
...
2
import requests
3
...
4
def randomimage(request):
5
r = requests.get('http://thecatapi.com/api/images/get?format=src&type=png')
6
return HttpResponse( r.content, content_type="image/png")
Copied!
To see the effects in the browser, the routing should be updated accordingly.
1
# Contents of config/urls.py
2
...
3
from sample.views import hello, myrandom, randomimage # <-- Updated
4
...
5
urlpatterns = [
6
path('admin/' , admin.site.urls),
7
url('randomimage' , randomimage), # <-- New
8
url('random' , myrandom),
9
url('' , hello),
10
]
Copied!
The browser sample output returned by a local iteration:
​

Resources

Last modified 5mo ago