#How do I add icon for every model in Django admin?

17 messages · Page 1 of 1 (latest)

carmine trellis
#

I using Tailwindcss custom admin dashboard style and i know it should changed in app_list.html, but i get nothing when i using {{ model.xxxxx }}
app_list.html

{% load i18n %}

{% if app_list %}
  {% for app in app_list %}
    <div class="app-{{ app.app_label }} module{% if app.app_url in request.path|urlencode %} current-app{% endif %}">
      <ul>
        <li>
          <a href="{{ app.app_url }}" class="section" title="{% blocktranslate with name=app.name %}Models in the {{ name }} application{% endblocktranslate %}">{{ app.name }}</a>
        </li>
        {% for model in app.models %}
          <li class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path|urlencode %} current-model{% endif %}">
            {% if model.admin_url %}
              <a class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700"
                 href="{{ model.admin_url }}"{% if model.admin_url in request.path|urlencode %} aria-current="page"{% endif %}>
                <i class="{{ model.get_icon }}"></i>
                {{ model.name }}
              </a>
            {% else %}
              {{ model.name }}
            {% endif %}
          </li>
        {% endfor %}
      </ul>
    </div>
  {% endfor %}
{% else %}
  <p>{% translate 'You don’t have permission to view or edit anything.' %}</p>
{% endif %}

admin.py

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', ]
    list_editable = ['name']

    @property
    def get_icon(self):
        return 'fa-solid fa-grip'

it display in console html:
<i class=""></i>

carmine trellis
#

🥹

runic hornet
#

ModelAdmin != model

carmine trellis
runic hornet
#

supposedly {{ model.model }} is a the Model class so it should be accessible

carmine trellis
#

The strange thing is that it doesn't get any text or html input, doesn't display anything, and also doesn't get any errors, I'm curious if this is an error or just doesn't work?

runic hornet
#

It's not an error, it's just how templates work

#

how did you try to refer it?

carmine trellis
# runic hornet It's not an error, it's just how templates work

I just use the {{ model.get_icon }} variable in the template and don't do anything else to set it up.

{% load i18n %}
...
              <a class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700"
                 href="{{ model.admin_url }}"{% if model.admin_url in request.path|urlencode %} aria-current="page"{% endif %}>
                {{ model.get_icon }}
                {{ model.name }}
              </a>
...

runic hornet
#

because {{ app.model }} is a dictionary it seems, not the actual Model

#

but should include based from the code

carmine trellis
carmine trellis
#

@runic hornet Is there any similar method that can be applied to the app name? I've tried everything online including changing APPconfig, rewriting the get_context(self,request) method with no success. Any suggestions would be appreciated!

runic hornet