How to view and define values for a model using the Django template

Asked

Viewed 16 times

1

Django has HTML files that we can override, one of them is called app_list.html, which has the following code:

{% if app_list %}
  {% for app in app_list %}
    <div class="app-{{ app.app_label }} module{% if app.app_url in request.path %} current-app{% endif %}">
      <table>
        <caption>
          <a href="{{ app.app_url }}" class="section" title="{% blocktranslate with name=app.name %}Models in the {{ name }} application{% endblocktranslate %}">{{ app.name }}</a>
        </caption>
        {% for model in app.models%}
              <tr class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path %} current-model{% endif %}">

This code iterates on the models of the apps we have registered, if you repair we can recover some attributes as in model.amin_url on line 9, and we can use conditionals on them.

What I’d like to know is: How do we get these attributes into models? I tried to perform these conditionals with models.pyand it didn’t work. I tried to add admin.py and it didn’t work either. No admin.pyput something like this:

@admin.register(TipoCurso)
class TipoCursoAdmin(admin.ModelAdmin):
    list_display = ('nome', )
    is_config = True

I would use that is_config to make a custom tab only with models that had it set as True,but unfortunately this soling did not work, someone would have an idea of how to define these attributes of models to be used in templates?

  • insert'object_name' attribute for example? I don’t quite understand... I already solved problem in template with Templatetag. Inserting an existing attribute in the template presentation can be done directly in the Meta class, you just need to know how to call the right function. In this case of presenting in tabs, I suppose q is a question more for Bootstrap than for Django, do something like {% if app.name == x %} and follow the code

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.