Table of contents
No headings in the article.
- This article is a continuation of my previous article.
- In this article:
- creating a django application
- url routing
- I too had the same question, why create an app yet in our previous article we already created a project? You could have a blog project with various apps under it. For instance an app for the blog and an app for maybe chatting on the blog .. and If you sold something .. an app for the store. But its all one project or website. Take an example of facebook , we would have an app for chatting, one for the marketplace and a different one for posting videos.Hope that makes it clear.
- Now let's shift to the directory with the
manage.py
file. Create an appliction like so.python manage.py startapp pykith_blog
- This creates an app under the name you specified, I used
pykith_blog
, replace that with what you used. - Now lets explore the newly created app directory
pykith_blog
.
- Don't be overwhelmed by the so many python files that have been created. We'll explore
views.py
first.
- The file already has some imports done
from django.shortcuts import render
. This we will use soon. - Delete the comment and add this line right under the first import.
from django.http import HttpResponse
- Lets move on to creating a view function to handle traffic sent to our home page. From this function we will decide what the user gets to see when they request for the home page. Add this function to our app's views.
def home(request): return HttpResponse('<h1> Blog Home </h1>')
The views module should now look like this
The function takes on one argument
request
that we ain't going to use for now. The home route will return an HttpResponse with the htmlBlog Home
.- Now we create the url pattern to map to this view function.
- In the app's directory (not project) , let's create a file under the name
urls.py
. This will be the app's url module. In here we will create the url patterns to correspond to the view functions inviews.py
. - This file will have alot in common with the
urls.py
file in the base project folder. Quickly take a look at the base project urls module.
- We realise from the file, django has imported
path
fromdjango.urls
. Then the url patterns list at line 19 using the path import.urlpatterns = [ path('admin/', admin.site.urls)]
. - Based on the code here, traffic from
[baseurl]/admin
will be sent toadmin.site.urls
. - Let's replicate this in the
urls.py
file we just created. - Let's add this code to our app's url's module.
from django.urls import path from . import views urlpatterns = [ path('', views.home, name='blog-home'), ]
- The first line is a copy paste from the base project's urls module.
- On the second line we import the
views
module from our current working directory, that explains the'.'
. - We move on to create a url pattern that maps an empty path, [something like
127.0.0.1/
], to our home page. That explains the""
in the first parameter given to the path function. - This will be handled by the home view function
views.home
that we created earlier in the views module file we just imported. - The last param is the name of the pattern, this will be important in future for reverse lookups .
- More on reverse lookups in the next article.
- All the user requests are handled by the base project's urls module and therefore we need a way to tell it to map urls headed for
blog/
ie127.0.0.1/blog
to the blog app's urls module for further routing. - A bit daunting , yeah but you'll get the hang of it :).. So let's go back to the base project's urls module and do a few additions.
- First import the include function , then add the following paths to the list of urlpatterns.
path('', include('pykith_blog.urls')), path('blog/',include('pykith_blog.urls'))
- Our base urls module should now look something like this after those few changes.
- Yes, I'm also tired but just a few more lines..., let's do a test run of the server to confirm everything is working fine so far.
- Back to the folder with
manage.py
and let's runpython manage.py runserver
to start the server.
- Now the base project urls module only sends the portion of the url that it has not matched to the included urls module. For instance we just accessed
127.0.0.1:8000/blog/
. The base project's urls module matchedblog/
in it's urlpatterns list.
- And sent what was left ie
'blog/' minus 'blog/' equals ''
, There for what is sent topykith_blog.urls
is an empty string. - In 'pykith_blog.urls' i.e. the app's urls module, django searches for the pattern that matches the empty string route.
- We realize there's a pattern that matches an empty route to the home page via the
views.home
function and thats how we finally get what is rendered in the browser. - Now let's add an about page just so to make this clearer. We'll follow the same process we used when adding the home page.
- First we add a view function to handle the about page logic. We'll call the function
about
. Go on paste the code below in that views file.def about(request): return HttpResponse('<h1> Blog About </h1>')
- Our views file right about now is something like this.
- Now we need to add a url pattern mapping to this views function in our app's urls.py file.
- Let's add this to the list of urlpatterns in the app's urls module.
path('about/', views.about, name='blog-about')
- The app's urls module now looks like.
- Yeah, it's starting to get easy, so the
about/
route will be handled by theviews.home
function defined in the pattern under the nameblog-about
. - Actually we don't need to edit a thing in the base project's urls module, When a user requests for
/blog/about
, the base project's urls module will look in its patterns for a route matching 'blog/' .
- It will then chop this off and send whats left, in our case,
about/
to the app's urls module. In here we will look for a pattern defined matching 'about/' .
- Finally the request will be sent to the
views.about
function.
- Here the
Httpresponse
will be returned with the htmlblog about
. - On saving that and running
python manage.py runserver
. You should get this when you visit the pathblog/about
.
- That was a long one.. wheew..... Let's pick up form here in the next article. If you found any of this useful don't forget to leave a comment. For any mistakes you spot out. Still a comment. And a reaction.. ✌️.