Answer the question
In order to leave comments, you need to log in
How to properly create your own context_processors in Django?
I'm sorry, but I didn't understand the documentation. I want to make the context available in all templates, but not rewrite all its variables in each view.
1. I create context_processors.py at the same level as settings.py:
from django.core.context_processors import request
def site(request):
return {'key':'arg',...}
TEMPLATES = [
{ ...
'OPTIONS': {
'context_processors': [
...
'context_processors.site'
],
},
},
]
Answer the question
In order to leave comments, you need to log in
Wangyu that the path is wrong. Try something like this 'project_name.context_processors.site'
You don't need to create your own context processor. In this situation, templatetags will do.
# Файл some_tags.py
from django import template
from monitoring.models import <Your model>
register = template.Library()
@register.simple_tag()
def get_<some>():
return <Your model>.objects.all()
# В файлах .html
{% load some_tags %} # Подгружаем теги
{% get_<some> as objects %} # Используем функцию, которую прописали в файле some_tags.py
{% for obj in objects %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question