part-2

Adding Views And Routes In Django

Atufa Shireen
3 min readMay 27, 2020

This chapter will teach you the following:

  • Adding urls and views to your project
  • Mapping the urls to your app

After setting up our project, now let’s create some views which displays the output on the page.

Head over to the views.py file which is in blog_project/blogapp and import the HttpResponse from django.http, so the views.py file will look like this..

from django.shortcuts import render
from django.http import HttpResponse

Now,let’s create a function which will handle the traffic from homepage and we’ll call it home.This function will return what we want to show the user on the homepage, So the views.py will be

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('<h1>Hi from the homepage</h1>')

We haven’t actually mapped our url pattern to this function,So to do that we’ll add a new file in blogs/ called urls.py (note that the urls.py will not be present in blog_project/blogapp , but will be present in the blog_project/ ).

Our blogs/urls.py file will be similar to the blog_project/url.py file.So I’ll copy some code from there and change the route from /admin to empty which will map to the homepage.Remember that the home function is in views.py file so i’ll import that from the ( . )or current directory as well.

The blog_project/urls.py will look like this..


from django.urls import path
from . import views

urlpatterns=[
path("",views.home,name="blog-home"), #empty path for home function
]

This wouldn’t work quite yet,because we have a urls.py file in our main project directory as well and that module will tell our whole website which urls should send us to our blog app

Let’s change the blog_project/urls.py to so (check the comment section provided above the imports by django)

from django.contrib import admin
from django.urls import path,include
urlpatterns=[
path("admin/",admin.site.urls),
path("blogs/",include('blogs.urls')),
]

The admin/ will direct to admin page and blog/ will direct to empty route of blogapp as in blogapp/urls which will map to views.home function.(That’s the reason why we created urls.py file in blogapp/ to have the app it’s own urls.)

Now,try running your server (make sure you’re in correct working directory blog_project/) with

python manage.py runserver

Oops! did you got a 404 Not Found too.

Let’s try debugging…Check the error,

Using the URLconf defined in blog_project.urls, Django tried these URL patterns, in this order:
admin/
blogs/
The empty path didn’t match any of these.

let’s go to blogs/

And you see the homepage saying..

Hi from the homepage

Since, we have only one app in our project let’s leave the app url empty(‘blogs/’ to ‘ ’)and then we wouldn’t need to write blogs/ to view the homepage.

Let’s add some more routes to our page

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('<h1>Hi from the homepage</h1>')
def about(request):
return HttpResponse('<h1>About me..</h1>')
def posts(request):
return HttpResponse('<h1>All posts here..</h1>')
def post(request):
return HttpResponse('<h1>A detailed post..</h1>')
def update(request):
return HttpResponse('<h1>Update your post..</h1>')
def deletepost(request):
return HttpResponse('<h1>Delete a post..</h1>')

As since,we have added more functions or routes we’ll need to map them to url as well, just as we did for the home function in the urls.py file.

from django.urls import path
from . import views

urlpatterns = [
path("", views.home, name="blog-home"),
path("about/", views.about, name="about"),
path("posts/", views.posts, name="posts"),
path("post/", views.post, name="post"),
path("update/", views.update, name="update"),
path("delete/", views.delete, name="delete"),

]

Next Up..

Previously In The Series..

--

--