Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing a Django admin template

I would like to modify an admin template in Django.

 % cat /Library/Python/2.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html 
<fieldset class="module aligned {{ fieldset.classes }}">
  {% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
  {% if fieldset.description %}<div class="description">{{ fieldset.description|safe }}</div>{% endif %}
  {% for line in fieldset %}
      <div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} ">
      {{ line.errors }}
      {% for field in line %}
      <div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>
          {% if field.is_checkbox %}
              {{ field.field }}{{ field.label_tag }}
          {% else %}
              {{ field.label_tag }}{{ field.field }}
          {% endif %}
          {% if field.field.field.help_text %}<p class="help">{{ field.field.field.help_text|safe }}</p>{% endif %}
      </div>
      {% endfor %}
      </div>
  {% endfor %}
</fieldset>

What kind of object is field, and more specifically how would I get the name of a field?


2 Answers

field is an instance of AdminField and field.field is an instance of BoundField, so you can reference the fields name with:

{{ field.field.name }}

Once as you start to dive deep into admin customisation, its the only place the documentation is really lacking. that being said, the code is well written and easy to understand if you take the time to research it, IMHO.

There is not many files so take an evening and read through them. In your case, I would start with:

  • contrib/admin/sites.py
  • contrib/admin/options.py
  • contrib/admin/helpers.py
like image 167
Gerry Avatar answered Nov 19 '25 20:11

Gerry


Have you done your research?

After that, I would start poring over the python code that invokes your template. I would imagine that field is from the forms system

Field A class that is responsible for doing validation, e.g. an EmailField that makes sure its data is a valid e-mail address.

like image 34
Tom Leys Avatar answered Nov 19 '25 19:11

Tom Leys