Hello Coders and Welcome back to the another tutorial of the series this is going to be third part and in this tutorial we are going to learn about views, how to create them and we will learn how to do URL routing
If you have not gone through the First and Second part of the series we stongly recommend you to go through them first. Links are given below.
In this tutorial we are basically going to build a page which a user can open in a web browser and can see all the blog posts which are published by the author, we have already explain views in the previous tutorials so we will not waste more time and we will straight dive into creating views but before that we will setup our URL at which our view will be opened so let's start.
Open the following directory in your browser BlogSystem\blogapp and create a new file called urls.py and type in the following code into it.
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$',views.index), url(r'^(?P<slug>[\w-]+)/$',views.show_article), ]
In the above code we are simply creating a url pattern which will represent http://localhost:8000/blogapp/ and http://localhost:8000/blogapp/slug-url-come-here which will tell the browser to open the index and show_article function defined inside the views.py.
Now go to the following directory BlogSystem\BlogSystem\urls.py and remove all the code present in it and type in the following code in it.
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/', include('blogapp.urls')), ]
In the above code we are telling django to route the url http://localhost:8000/blog to blogapp\urls.py where we have defined a view for the specified url.
Now we have successfully routed our url now lets define function called index which we have defined previously in our urls.py in blogapp/views.py, now delete all the code present in blogapp/views.py and type in the below code into it.
from django.http import HttpResponse def index(request): return HttpResponse("<h1>This will be the blog</h1>")
The above index function will send a simple HttpRequest defined inside it to the server and will display the output. now open your django server by typing the following command in to the terminal(do not forget to locate your project directory into the terminal)
python manage.py runserver
Now go to the following url http://localhost:8000/blog and you will see result as shown below.
Now in plca of the above text we will show the headings and description of our articles, so go to the following file BlogSystem/blogapp/views.py and modify the code as shown below
from django.http import HttpResponse from .models import Article #importing Article model from models.py def index(request): all_data = Article.objects.all() #fetching data into an object from model Article html = '' for data in all_data: #looping through the entire Article model url = data.slug html += '<h1>'+data.title+'</h1><br /><a href="'+url+'/">Read more</a>' return HttpResponse(html) def show_article(request,slug): all_data = Article.objects.filter(slug=slug) #fetching data into an object from model Article where slug = the slug variable html = '' for data in all_data: #looping through the entire Article model url = data.slug html += '<h1>'+data.title+'</h1><br /><p>'+data.content+'</p>' return HttpResponse(html)
In the above code we have comment out some important line and what these lines are actually doing, For function index first we are making an object called all_data and fetching all data from the database using
Article.objects.all() which will fetch all the database and then using the for loop we are displaying the data using the object which we created.
In show_article function we are passing an argument called slug from the URL pattern which we defined previously, the function Article.objects.filter() will filter the result for those article having slug field equal to the slug value passed from the URL.
Now if you have followed all the steps properly you will have something as shown below.
Thats it for now, in the next tutorial we will design a template and a UI for our blogging System and will finalise our project.
Thank you for reading.
Also, don't forget to subscribe to our Newsletter.
If you like this article, then please share it and help us grow.