I was wondering if there's any way of sending a default context for all pages in django, such as user is always passed to the template regardless of other context, for my particular case i want to send context for navbar such as category and sub category to all pages without having to send in all the views. TIA
You can achieve this by creating a context processor. Create a context_processors.py in any app, I suggest doing it on your main app. And here you can create context dictionaries. Like so:
from product.models import SubCategory, Category
def add_variable_to_context(request):
return {
'subCategories': SubCategory.objects.order_by('id').all(),
'categories': Category.objects.order_by("id").all(),
}
Add this line to your TEMPLATES in settings.py to call the created context processor
'OPTIONS': {
'context_processors': [
....
'yourAppName.context_processors.add_variable_to_context',
....
],
},
Now you can call your created context dictionaries in your base.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.