We will continue to build our blog app where we left in the post writing-your-first-blog-app-in-django-part-8.
So in this post we are going to construct our article detail page. Upto now we have our all urls hooked up,so that when we click on the title of any article it will take us to the page which contain individual article page.
In the view we are receiving slug and returning that slug only,instead of that ideally what we had like to do is to query the database to find an article based on this slug.Remember whenever we create an article a slug which is at the url,so what we can do is query the database to find a particular article with that slug and if it exists we grab it and we send that back to a template to display that particular article instead of just sending back the slug.So that’s what we are going to do now:
So to do that first change the article_details function in articles/views.py function as following:
from django.shortcuts import render from .models import Article from django.http import HttpResponse ''' Takes in the request object. All we are going to do in this function is we will render a template to the user which is going to show a list of articles. First parameter in render is always the request object and the second parameter is the template we want to render and third optional is the directory ''' def articles_list(request): #Retrieve all articles from database and sort them according to date articles = Article.objects.all().order_by('date') return render(request, 'articles/articles_list.html',{'articles':articles}) def articles_details(request, slug): #return HttpResponse(slug) article = Article.objects.get(slug=slug) return render(request,'articles/article_detail.html',{'article':article})
So what we are doing is we are finding in the database which article have matching slug,if we find matching slug we will send that article to articles_detail.html template.
Till now we did not created the article_detail.html.So first create article_detail.html template inside templates folder inside articles app as following:
After creating template add following content inside that article_detail.html template.
{% extends 'base_layout.html' %} {% block content%} <br> <br> <div class="article-detail"> <div class="article"> <h2> {{ article.title }} </h2> <p> {{ article.body }} </p> <p> {{ article.date }} </p> </div> </div> {% endblock %}
So after doing all these changes,run the server again.Now after server running on the page which is showing articles_list if you click on any article title it will open the page that contain all detail about that particular article,on title you clicked.
Suppose we click on testaroo title it will open this page which will contain detail about testaroo article as following:
So now we have reached our both goals like showing all of the articles on the one page and if user wants to know further about particular article he/she can click on the title of that particular article about which he/she wants to know.It will open the page that contains full detail about that article.
In the next post we are going to see how we can upload media so that we can display images in the articles also.Check out the next post for further details
So in this post we have added a view by which user can check detail about particular article.