Reputation: 157
I've created custom index.html for django admin. Here is the ScreeenShot
Here count of user or model 1 is static. I want to update from database. Is there any way to pass variable from admin.py to main Admin interface?
Is there any way to change in index just like we can do with some model : -
class UserAdmin(DjangoUserAdmin):
"""Define admin model for custom User model with no email field."""
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name','Mobile_number','Address','avatar','AI','U_ID')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2'),
}),
)
list_display = ('email', 'first_name', 'last_name', 'is_staff','Mobile_number','Address','avatar','AI','U_ID')
search_fields = ('email', 'first_name', 'last_name','Mobile_number','Address','avatar','AI','U_ID')
ordering = ('email',)
Upvotes: 0
Views: 1409
Reputation: 51988
One idea is to use custom context-processor
. You can add a new context processor like this:
# in context_processor.py
def user_count(request):
return { 'total_user' : User.objects.all().count() }
register it in settings.py
inside TEMPLATE
configurations:
'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',
'your_app.context_processor.user_count'
],
},
Use it inside template:
<div>
{{ user_count }}
</div>
context-processor
is helpful when you want to show same data in every single template. But if this data is only for one page and maybe needs to send extra context data via modeladmin, then change_view is more suited for this. For example(copy pasted from documentation):
class MyModelAdmin(admin.ModelAdmin):
change_form_template = 'admin/myapp/extras/openstreetmap_change_form.html'
def get_osm_info(self):
pass
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['osm_data'] = self.get_osm_info()
return super().change_view(
request, object_id, form_url, extra_context=extra_context,
)
Upvotes: 3