In this Django tutorial we are going to talk about Django get_object_or_404(), it calls get() on a given model manager, but it raises Http404
instead of the model’s DoesNotExist
exception. also we are going to learn that how you can create links for your Question model items that is retrieved from the database.
OK now first if all we need to create a new view function, so open your views.py file and add this code, so you can see that we have used get_object_or_404() method, it will return page not found if there were not any data.
1 2 3 4 5 |
def Poll_Details(request, poll_id): poll = get_object_or_404(Question, id = poll_id) context = {'poll':poll} return render(request, 'poll_details.html', context) |
This is our complete views.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from django.shortcuts import render from .models import Question from django.shortcuts import get_object_or_404 # Create your views here. def Index(request): context = {} return render(request, 'index.html', context) def Polls(request): polls = Question.objects.all() context = {'polls':polls} return render(request, 'polls_list.html', context) def Poll_Details(request, poll_id): poll = get_object_or_404(Question, id = poll_id) context = {'poll':poll} return render(request, 'poll_details.html', context) |
Now create a new html file for the details at name of poll_details.html, and you can see that we have rendered the data in here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{% extends 'base.html' %} {% block title %}Polls Details{% endblock %} {% block body %} <div class="container"> <h1>Poll Details</h1> {{poll}} </div> {% endblock %} |
Also we need to create a new url, so open your polls app urls.py file, and add this.
1 |
path('details/<int:poll_id>/', Poll_Details, name = 'poll_details') |
So this is the complete urls.py file.
1 2 3 4 5 6 7 |
from django.urls import path from .views import Index, Polls, Poll_Details urlpatterns = [ path('', Index, name = 'index'), path('polls/', Polls, name = 'polls_list'), path('details/<int:poll_id>/', Poll_Details, name = 'poll_details') ] |
Now go to http://localhost:8000/details/2/ and this will be the result.

Now we want to add links to our Question data items, so open your polls_list.html and add this code.
mysite/templates/polls_list.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
{% extends 'base.html' %} {% block title %} Polls List {% endblock %} {% block body %} <div class="container"> <h1>Polls List Page</h1> {% for poll in polls %} <ul class="list-group"> <li class="list-group-item"> <a href="{% url 'poll_details' poll.id%}">{{poll}}</a> </li> </ul> {% endfor %} </div> {% endblock %} |
This will be the result and you can see that we have links in our items.

Rest of the file for the project.
mysite/templates/navbar.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<nav class="navbar navbar-expand-lg navbar navbar-dark bg-dark"> <a class="navbar-brand" href="{% url 'index' %}">GeeksCoders</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a> <a class="nav-link" href="{% url 'polls_list' %}">Polls</a> </div> </div> </nav> |
mysite/templates/base.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!DOCTYPE html> {% load static %} <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}</title> <!-- CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" > <link rel="stylesheet" href="{% static 'css/style.css' %}"> </head> <body> {% include 'navbar.html' %} {% block body %} {% endblock %} <!-- jQuery and JS bundle w/ Popper.js --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" > </script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/ bootstrap.bundle.min.js" ></script> </body> </html> |
mysite/templates/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{% extends 'base.html' %} {% block title %} Home {% endblock %} {% block body %} <div class="container"> <h1>Welcome to GeeksCoders.com</h1> <p>Tutorial Number 14 - Django get_object_or_404()</p> </div> {% endblock %} |
mysite/static/css/style.css
1 2 3 4 5 6 7 8 9 10 11 |
h1 { color:green } p { color:red } |
mysite/urls.py
1 2 3 4 5 6 7 |
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('polls.urls')) ] |