Django how to pass a variable to the template in admin?

How to pass gallery_add in the template?

admin.py
class Service_Admin(admin.ModelAdmin):
save_as=True
 change_form_template = "admin_template.html"
 from django.db import connection
 with connection.cursor() as cursor:
 cursor.execute("SELECT * FROM some_table ")
 gallery_add = cursor.fetchall()


admin_template.html:
{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}

{% block inline_field_sets %}

 {% for inline_admin_formset in inline_admin_formsets %}
 {% include inline_admin_formset.opts.template %}
 {% ifequal inline_admin_formset.formset.prefix 'block_name' %}
 <select id='add_id'>
 <b>{% for line in gallery_add %}
 <option value='{{ line.id }}'>{{ line.name }}</option>
 {% endfor %}</b>
</select>
 {% endifequal%}
 {% endfor %}
{% endblock %}
April 19th 20 at 12:41
1 answer
April 19th 20 at 12:43
Solution
The problem is solved through the definition of a change_view and indicate there extra_context

class Service_Admin(admin.ModelAdmin):
 change_form_template = "admin_template.html"
 def change_view(self, request, object_id, form_url=", extra_context=None):
 extra_context = extra_context or {}
 extra_context['gallery_add'] = "Desired value"
 return super(Service_Admin, self).add_view(request, form_url, extra_context)


In the template to change nothing.

Find more questions by tags Django