I have a Django template, let's call it index.html which is divided in 3 parts (header, content and footer). In the header section I have a search bar that includes a drop-down menu which allows the user to select an option from it and search things based on the selected option. I want that header section to be include in all my future views/templates and still display the drop-down menu with all the options.
This is what I currently have in my view file
def index(request):
return render(
request,
'home.html',
{'categories': get_all_categories()}
)
def cart(request):
return render(request, 'cart.html', {'categories': get_all_categories()})
def help(request):
return render(request, 'help.html', {'categories': get_all_categories()})
def about(request):
return render(request, 'about.html', {'categories': get_all_categories()})
def contact(request):
return render(request, 'contact.html', {'categories': get_all_categories()})
def search(request):
return render(request, 'search.html', {'categories': get_all_categories()})
def get_all_categories():
return Category.objects.all()
This is what I have cart.html {% extends "index.html" %}
{% block content %}
<div>
<h1> My Cart </h1>
</div>
{% endblock %}
This is what contact.html has {% extends "index.html" %}
{% block content %}
<div>
<h1> Contact </h1>
</div>
{% endblock %}
This is what home.html contains {% extends "index.html" %}
{% block content %}
<div>
<h1> Home </h1>
</div>
{% endblock %}
This works right now but I was wondering if there was a better way to solve this so that I don't have to repeat the same code in all of the views.