Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django how to pass custom variables to context to use in custom admin template?

I am extending change_list.html and I need to output a variable defined in settings.py.

How do I pass that particular variable into the custom admin template context?

like image 413
James Lin Avatar asked Feb 09 '12 22:02

James Lin


People also ask

How do you pass variables from Django view to a template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

Is Django's admin interface customizable if yes then how?

In this article, we will discuss how to enhance Django-admin Interface. Let us create an app called state which has one model with the same name(state). When we register app to admin.py it shows like. Now lets' customize django admin according to available options.

What is Admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


1 Answers

class MyModelAdmin(admin.ModelAdmin):     ...     def changelist_view(self, request, extra_context=None):         extra_context = extra_context or {}         extra_context['some_var'] = 'This is what I want to show'         return super(MyModelAdmin, self).changelist_view(request, extra_context=extra_context) 

See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.changelist_view

like image 143
Chris Pratt Avatar answered Sep 23 '22 01:09

Chris Pratt