Very good friends, I have my custom context processor, so that it can be used in all the django project templates in the form of a variable:
{{ u_model }}
This is my context processor:
proccesors.py
from django.contrib.auth.decorators import login_required
@login_required(redirect_field_name='login')
def ctx_dict(request):
u_model = Model.objects.all().filter(user=request.user)
ctx = {
'u_model':u_model,
}
return ctx
This added in settings as it should be:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'my_app.proccesors.ctx_dict',
],
},
},
]
template.html
{% if request.user.is_authenticated %}
{% for x in u_model %}
{{ x.atributo }} # Es un campo de mi modelo Model
{% endfor %}
{% else %}
No logueado
{% endif %}
Everything works correctly when the user is logged in, but when he closes his session I see the following error:
ValueError: dictionary update sequence element # 0 has length 0; 1 is required
Then I do not know what is due. Thank you very much friends.