2

I want to pass a list of object to my custom template in Djando admin, delete_view and change_view. I modified base_site.html for including a nav-bar and I want to pass, from view, a list of objects to the nav-bar.

I did it before for django admin index, changelist_view and add_view as well. I override those function and add the extra content I want.

But I can´t do it in the two firts. The idea should be this one:

@csrf_protect_m
@transaction.atomic
def delete_view(self, request, object_id, extra_context=None):
    extra_context = extra_context or {}                 
    mygetModels = getModels()
    extra_context["modelsTables"] = mygetModels.getTablesModels()
    return super(table_NameAdmin, self).delete_view(request, object_id, extra_context)

As I said that works before in changelist_view, index and add_view. But is not working in delete_view and change_view.

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
  • 1
    Try writing a [custom template tag](https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/#simple-tags), and include it in your admin template. That way, you won't have to override every single admin view just to add some items to the template context. – Alasdair Apr 05 '16 at 08:39
  • Well... Looks better indeed. So I define my function as: @register.simple_tag def getTablesModels(self): Classes = apps.apps.get_models(ddbb) modelClasses = [] for i in xrange(len(Classes)): obj = dbModels(Classes[i].__name__.lower(), Classes[i]._meta.db_table) modelClasses += [obj] return modelClasses How I call it on My index admin template? Should I import something? I need to go through all list which this tag is returning {% for item in modelsTables %}
  • a href="/admin/db/{{ item.linkDb }}/">{{ item.nameDb }}
  • {% endfor %} – Kenzo_Gilead Apr 05 '16 at 08:56
  • Please don't post code in the comments, it's impossible to read. – Alasdair Apr 05 '16 at 08:58
  • Hahahaha... lol. Your right. My fault... Anyway, your second approach is better. Cleaner, smarter,... Thanks Alasdair. – Kenzo_Gilead Apr 05 '16 at 09:09