2

My admin.ModelAdmin have a media class like this:

 class AdminForm(admin.ModelAdmin):
      class Media:
              js = ('admin/js/admin.js', static('js/tags.js'),
                    'http://maps.google.com/maps/api/js?sensor=false')

But I want to pass variables to the admin.js, how can I do it?

Thanks

dev-jim
  • 2,404
  • 6
  • 35
  • 61

1 Answers1

1

Possible solution:
Override admin template, pass extra context via https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.changelist_view (see Django how to pass custom variables to context to use in custom admin template? for code example), and in the template you will have something like this:

<script>
    var yourVar = {{ var_from_extra_context }};
</script>

Then in your included javascript files you can use these variables.
Note: this code should be before js imports.

Community
  • 1
  • 1
coldmind
  • 5,167
  • 2
  • 22
  • 22