0

I am currently in the process of adding a theme to the admin of django one issue I have found it adding styling to the forms is that it is very difficult and I can't find much useful documentation on it. The only real thing I need to do is add classes to the form elements so that they match the theme I am using is this possible and if so how would you go about doing it the code I am currently using is very basic and the basic code included in the standard theme does anybody know how to add classes to these standard bits of code bellow is what I have.

  {% if is_popup %}
        <input type="hidden" name="_popup" value="1" />
  {% endif %}
  {% if save_on_top %}
        {% block submit_buttons_top %}
            {% submit_row %}
        {% endblock %}
  {% endif %}
  {% if errors %}
        <p class="errornote">
                {% blocktrans count counter=errors|length %}
                     Please correct the error below.
                     {% plural %}
                     Please correct the errors below.
                {% endblocktrans %}
        </p>
        {{ adminform.form.non_field_errors }}
  {% endif %}

 {% block field_sets %}
        {% for fieldset in adminform %}
                {% include "admin/includes/fieldset.html" %}
        {% endfor %}
 {% endblock %}

 {% block after_field_sets %}{% endblock %}

 {% block inline_field_sets %}
        {% for inline_admin_formset in inline_admin_formsets %}
                {% include inline_admin_formset.opts.template %}
        {% endfor %}
 {% endblock %}

 {% block after_related_objects %}{% endblock %}

 {% block submit_buttons_bottom %}
        {% submit_row %}
 {% endblock %}

1 Answer 1

1

There are a lot of ways to do this, but certainly one way would be to overwrite all the widgets in your ModelAdmin rather than in the template. That could look something like this:

from django.db import models
from django.contrib import admin
from django.forms.extras.widgets import TextInput

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': TextInput(attrs={'class':'my-widget-class'},)},
    }

You'd have to go through and do that for each widget, but then they'd have the appropriate classes -- at least for that modelAdmin.

Sign up to request clarification or add additional context in comments.

7 Comments

Just wondering but where would the best place be to put this file does it need to go in an specific place.
Yeah, this code would have to go in the admin.py file for whatever relevant app. I assume most of your models have a modelAdmin in their admin.py file, so you would want to add the formfield_overrides for each of those. More information [in the docs].(docs.djangoproject.com/en/dev/ref/contrib/admin/…)
OK so there is no way to do a global change I can only change it for each individual model.
Oh, no. I'm not going to go so far as to say there is NO way to do it globally. But one didn't immediately come to my mind, and as a stopgap measure this method should work.
Ok thanks sorry to bother you again but what if I want to apply this to a models like the default user model
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.