Django Tutorial
About Lesson

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.

 

 

This is our complete views.py file.

 

 

 

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.

 

 

Also we need to create a new url, so open your polls app urls.py file, and add this.

 

 

So this is the complete urls.py file.

 

 

 

Now go to http://localhost:8000/details/2/ and this will be the result.

Django get_object_or_404()
Django get_object_or_404()

 

 

 

 

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.

 

 

 

This will be the result and you can see that we have links in our items.

Django Links Item
Django Links Item

 

 

Rest of the file for the project.

 

mysite/templates/navbar.html 

 

 

 

mysite/templates/base.html 

 

 

 

 

mysite/templates/index.html 

 

 

mysite/static/css/style.css

 

 

mysite/urls.py

 

 

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found