I need to do additional operations and the standard values from request.login for the base template from which the others inherit are not enough, so this solution came to mind:
views.py:
def base(view):
def response(request, view=view):
return view(request)
return response
urls.py:
from . import views
urlpatterns = [
path('', views.base(view=views.index), name='index'),
But I’m not really sure if it’s “right”, so I’d like to ask you how to implement my task without strong crutches (I also thought of writing middleware, but I’m not sure if it’s worth it for this task).
If you have data that needs to be added to the context when many different pages are being rendered, then you should be looking at context processors.
See the docs page starting at The Django template language: for Python programmers | Django documentation | Django, but primarily focusing on the sections about context processors and writing your own context processor.
1 Like
Thanks, what is the correct app file to add it to?
Quoting directly from the docs at Writing your own context processors:
Custom context processors can live anywhere in your code base. …
Having said that, we follow the convention used by Django itself and put our context processors in a file named context_processors.py
- but that’s a choice we made.
1 Like