Lesson: Django Tutorial – Django Url Tag
About Lesson

In this Django Tutorial we are going to learn about Django Url Tag,  django url tag returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates.

 

 

So first we need to create a new view function for our polls and after that we are going to link the urls in our navbar.html using django url tag. so this is our views.py file.

 

 

mysite/polls/views.py

 

 

Now create a new html file at name of polls_list.html for our newly created view.

 

mysite/templates/polls_list.html

 

 

 

And now this is our urls.py, you need map your newly view function. you can see in this code we have name parameter in our paths, we can use this name with url tag to create absolute path reference.

 

 

 

Now open your navbar.html file and in the href section you need to add this code, you can see that we have used django url template tag for adding a path reference for our urls.

 

 

Now this is our complete navbar.html.

 

 

 

This is our rest of files for our django project.

 

 

mysite/templates/index.html

 

 

 

mysite/static/css/style.css

 

 

 

mysite/polls/admin.py

 

 

 

mysite/polls/models.py

 

 

 

mysite/urls.py

 

 

 

Run your project and click on the Polls from navbar and this will be the result.

Django Tutorial - Django Url Tag
Django Tutorial – Django Url Tag

 

 

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
0% Complete