Code

Opened 6 years ago

Closed 3 years ago

#6636 closed (fixed)

change_form.html template should have a block around fieldsets

Reported by: peschler Owned by: nobody
Component: contrib.admin Version: 1.3
Severity: Keywords: newforms admin template nfa-someday
Cc: jpichon Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The default admin/changeform_html should be more granular concerning blocks or at least contain a block around the fieldsets to prevent code duplication. This applies to trunk and the newform-admin branch.

Here's a more elaborate explanation of the problem:

The admin/change_form.html template of the newforms-admin branch offers a variety of blocks to override. Unfortunately the main content block contains some areas which cannot be overwritten without duplicating a lot of code. Basically the block structure in the original newforms-admin change_form.html looks like this.

    {% block content %}        
        {% block object-tools %}some tools here...{% endblock object-tools %}

        # code for fieldsets (without block!)
        
        {% block after_field_sets %}{% endblock %}

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

        {% block after_related_objects %}{% endblock %}        
    {% endblock content %}

As you can see there are some blocks for adding stuff behind the fieldsets and the related objects (blocks after_field_sets and
after_related_objects) but unfortunately there is no way to overwrite the fieldsets because they are not contained within any block.

So why deal with that? If you want to overwrite the fieldset stuff in the above template, you *must* overwrite the content block. Thereby you loose all blocks within the content block (is this a bug or a feature?!) And that simply means you cannot do this:

    {% block content %}
        My new content!
        
        {% block object-tools %}
            {{ block.super }}           # does not work!
        {% endblock object-tools %}

        My new field stuff here...
        ...
    {% endblock content %}

This snippet tries to add some content before and after the object tools and
at the same time reuse the object-tools block from the parent template by
calling block.super. But that fails and the object_tools block will be
empty as will all child blocks of content. Thus the only choice is to
duplicate the *whole* content code, which is not DRY.

We can workaround this problem by restructuring the original
change_form.html and putting the fieldsets into their own block like
this::

    {% block content %}        
        {% block object-tools %}some tools here...{% endblock object-tools %}
        
        {% block field_sets %}
            # code for fieldsets (this time with block)
        {% endblock %}

        {% block after_field_sets %}{% endblock %}

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

        {% block after_related_objects %}{% endblock %}
{% endblock content %}

By making the template more granular, we can now simply overwrite the field_sets
block instead of the content block and all other blocks within
content stay intact.

Attachments (1)

6636.diff (1009 bytes) - added by jpichon 3 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 6 years ago by Karen Tracey <kmtracey@…>

  • Keywords nfa-someday added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Stated in description this applies to both trunk and nfa, should not block nfa merge.

comment:2 Changed 6 years ago by edgarsj

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by jpichon

  • Cc jpichon added

Is there something preventing this patch from being applied? I'm attaching a diff of peschler's patch against the latest change_form.html in trunk so it can be applied more easily. I also added a block to inline fieldsets, for the same reasons than in the initial report. Let me know if there's something else I can do to help with this?

Changed 3 years ago by jpichon

comment:4 Changed 3 years ago by russellm

  • Triage Stage changed from Accepted to Ready for checkin
  • Version changed from newforms-admin to 1.3

The only thing holding up this ticket is the fact that nobody has ever reviewed it and marked it ready for checkin. It's a simple fix, so I'll do that now.

comment:5 Changed 3 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

In [15925]:

Fixed #6636 -- Added template blocks to admin change_form.html template for fieldsets and inline fieldsets. Thanks, peschler and jpichon

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.