Django Tutorial
About Lesson

In this Django Tutorial we are going to talk about Django Include Tag, Include tag loads a template and renders it with the current context. this is a way of “including” other templates within a template. the template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes. for example in our application we want a navigation bar, now you can create a new template and include that template in your base.html

 

 

We want to create a navbar.html in our templates folder, and we are going to bring the bootstrap nav bar code from our base.html in to navbar.html.

 

 

mysite/templates/navbar.html

 

 

 

Now you can use django include tag for including your navbar.html, we want to include this file in our base.html. because we want the navbar for all the pages that are extending from our base html file.

 

 

 

So this is our base.html file and we have added the include tag in our body tag.

 

 

 

Now these are our rest of files for the project.

 

 

mysite/templates/index.html

 

 

 

mysite/polls/views.py

 

 

mysite/polls/models.py

 

 

mysite/polls/admin.py

 

 

mysite/static/css/style.css

 

 

mysite/urls.py

 

 

 

mysite/polls/urls.py

 

 

 

Run your django project and go to http://localhost:8000/, and this will be the result, you can see that how easily we have included our navbar.

Django Tutorial - Django Include Tag
Django Tutorial – Django Include 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