Prevent Django DetailView from adding template context with name of object
I have a DetailView
for users, and it seems to be adding a user
context variable. This is problematic because I have an application-wide context variable, user
that contains the current user that is being overridden. How can I prevent the DetailView from adding this context variable?
Override the get_context_data
method in your DetailView
and remove or modify the user variable from the context
from django.views.generic import DetailView
class MyDetailView(DetailView):
model = YourModel
template_name = 'your_template.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# remove the 'user' context variable if it exists OR
if 'user' in context:
del context['user']
# alternatively, you could update it to use your own user context variable
# context['user'] = self.request.user
return context