max
max

Reputation: 10444

django - how to override admin templates

I want to change the way the add new object page looks in the admin page. I do not want to change the fields but want to arrange the inputs, for example 2 fields on the firt line, then some paragraph then two other fields. I read about add_form_template and my guess is that this is supposed to allow me to change the template without defining the form. Here is my attempt which does not show the fields: Does anyone know whether I need to define the form and pass it in and how? If I have to pass in the form, then what is add_form_template used for?

from settings import BASE_DIR
import os

@admin.register(Owner)
class OwnerAdmin(admin.ModelAdmin):
    add_form_template = os.path.join(BASE_DIR, 'dealer/templates/add_owner.html')
    list_display = ('name', 'country', 'city', 'car')


@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    list_display = ('name',)

//----- add_owner.html
{% extends "admin/base.html" %}

{% block content %}
    <h1>New Owner</h1>
    <form method="POST" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Create</button>
    </form>
{% endblock %}

Upvotes: 0

Views: 2100

Answers (1)

ohrstrom
ohrstrom

Reputation: 2970

You don't have to change the admin templates to customize your field's layout. Use fieldsets instead:

# admin.py
@admin.register(Owner)
class OwnerAdmin(admin.ModelAdmin):
    # ...
    fieldsets = (
        (None, {
            'fields': (
                ('name', 'country'),
                'city'
                )
        }),
        ('Another Fieldset', {
            'description': 'Some information to display...',
            'fields': (
                'car', 
            ),
        }),
    )

Upvotes: 1

Related Questions