Django How to Use Include Template Tag

This is our seventh article in Django, in this article we are going to talk about How to Use Include Template Tag in Django. 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.

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

OK first of all you need to create a project in Django, we have already covered the creation of the Django project in our previous article Django Introduction & Installation. but you can use this command for creating of Django Project.

 

 

 

After creation of the Django Project, you need to migrate your Django Project.

 

 

 

Now we need to create an App, you can read the complete article about Django Apps in this link  Django Apps & URL Routing . i have named my App as news, but you can name it what ever you want.

 

 

 

Also you need to add your newly created app in the Django settings.py INSTALLED_APPS. make sure that always do this process after creating a new App.

 

 

 

 

OK before this we have talked that about  how you can use templates in django, you can check this article Django Templates Introduction. but you need to just create a templates folder in your project and you need to add some html files. basically we are going to create three html files. and also don’t forget to add your templates folder in your settings.py DIRS Section. also we have talked about template inheritance in django,  Django Template Inheritance.

 

 

 

 

Iam going to create a new html file, called navbar.html.

 

So now i want to include my navbar.html in the base.html, because i want the navbar for all my all pages.

 

 

 

OK now this is our base.html. and we have included an external html file at name of navbar.html, that we have already created.

 

 

And these are our three html files for the pages that we are going to use.

 

 

 

home.html

 

 

 

about.html

 

 

 

 

contact.html

 

 

 

 

So now open your views.py and we need to add our view functions.

 

 

 

 

Also you need to create your urls, you can read this article for url routing Django Apps & URL Routing . but just create a new python file in your news app at name of urls.py and add these codes.

 

 

 

And also you need to include your app urls.py in your project urls.py file.

 

 

 

 

So now you can run your project and see the urls, and there is our simple navbar in every page.

 

 

 

 

http://localhost:8000/

Django How to Use Include Template Tag
Django How to Use Include Template Tag

 

 

 

 

http://localhost:8000/about/

Django Include Tag About
Django Include Tag About

 

 

 

 

http://localhost:8000/contact/

Django Include Tag Contact
Django Include Tag Contact

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×