Rectangle 27 1

The problem is this (and similar) lines:

(None, {'fields': ['agrees'], 'classes': ['collapse']}),

When the fieldset title is None, there is no fieldset title and thus nothing to click on to expand the fieldset. This comment in the Django bug tracker, albeit old, indicates that using no title and collapse is a Bad Idea (tm) and you "just shouldn't do that."

As for your other problem, there's a problem of some form with your OS. It just means that a connection (to your Python server) was made, but your computer (your host) terminated the connection before Python was done with it for some reason.

Django Admin Kink with Collapse Fieldsets - Stack Overflow

django django-admin
Rectangle 27 1

The problem is this (and similar) lines:

(None, {'fields': ['agrees'], 'classes': ['collapse']}),

When the fieldset title is None, there is no fieldset title and thus nothing to click on to expand the fieldset. This comment in the Django bug tracker, albeit old, indicates that using no title and collapse is a Bad Idea (tm) and you "just shouldn't do that."

As for your other problem, there's a problem of some form with your OS. It just means that a connection (to your Python server) was made, but your computer (your host) terminated the connection before Python was done with it for some reason.

Django Admin Kink with Collapse Fieldsets - Stack Overflow

django django-admin
Rectangle 27 0

You'll need to use JavaScript to toggle the visibility of fieldsets from a client-side click event. jQuery's toggle function should work quite nicely for this.

css - Django - how to collapse forms like in the django admin - Stack ...

css django django-forms
Rectangle 27 0

I do something like this, where I have one or more fields are only required based on the value of another. This is usually a checkbox or select, where true/false or one particular value means that we should show this other set of fields. I've added something like this:

$(document).ready(function(){
    function show_hide() {
        var is_checked = $('#id_first_field').attr('checked');
        $('.second_field')[is_checked ? 'show' : 'hide']();
    }
    show_hide();
    $('#id_first_field').change(show_hide);
});

python - How To Collapse Just One Field in Django Admin? - Stack Overf...

python django django-admin
Rectangle 27 0

Accoding with the grappelli docs only need to add "classes" : ('grp-collapse grp-closed')

class EntryOptions(admin.ModelAdmin):
   ...
   fieldsets = (
     ('', {
        'fields': ('title', 'subtitle', 'slug', 'pub_date', 'status',),
     }),
     ('Flags', {
        'classes': ('grp-collapse grp-closed',),
        'fields' : ('flag_front', 'flag_sticky', 'flag_allow_comments',),
    }),

note: if you use grappelli version < 2.4 use 'grp-closed' instead 'collapse-closed'* actually 'collapse-close' still is working but is recommended to use the new classes

Django Admin - add collapse to a fieldset, but have it start expanded ...

django django-admin
Rectangle 27 0

If you want to toggle the field display on click, you should use javascript. See: How To Collapse Just One Field in Django Admin?

If not, you can create or modify your form dynamically using base_fields in __init__:

if not self.instance.is_virtual:
        ServerAdminForm.host = django.db.models.ForeignKey(u'self', blank=True, null=True)
        ServerAdminForm.base_fields['host'] = ServerAdminForm.host

Thanks. I will look into that tomorrow when I get to work. Any ideas on why the html of the field does not have the attributes disabled or readonly?

The javascript method worked great, Thanks. I am still curious why my above approach was failing though.

Django ModelChoiceField not adding attrs to html in admin - Stack Over...

django-admin django-forms
Rectangle 27 0

The way I did it is simply use the collapse.js script of django admin.

In the template, import the script:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/actions.js"></script>
<script type="text/javascript" src="/static/js/collapse.js"></script>

Then change the tag <legend> for <h2> and bingo, you have the same collapse.

<form method="post" action="." encrypt="multipart/form-data">{% csrf_token %}
    <b>App name: {{ App_title }}</b>
    {% if formset.non_field_errors %}{{ formset.non_field_errors }}{% endif %}
    {% for fieldset in formset.fieldsets %}
        <fieldset class="{{ fieldset.classes }}">
        {% if fieldset.legend %}
            <h2>{{ fieldset.legend }}</h2>
        {% endif %}
        {% if fieldset.description %}
        <p class="description">{{ fieldset.description }}</p>
        {% endif %}
        <ul>
        {% for field in fieldset %}
            {% if field.is_hidden %}
                {{ field }}
            {% else %}
                <li{{ field.row_attrs }}>
                {{ field.errors }}
                {{ field.label_tag }}<br>
                {{ field }}
                </li>
            {% endif %}
        {% endfor %}
        </ul>
        </fieldset>
    {% endfor %}

css - Django - how to collapse forms like in the django admin - Stack ...

css django django-forms
Rectangle 27 0

Starting from Setomidor answer, I'd like to suggest a simpler alternative that does exactly what you want (if Grappelli is not an option, obviously).

admin/(app)/(model)/change_form.html

instead of removing the collapsible effect

call the click method of the field-set collapser

Django Admin - add collapse to a fieldset, but have it start expanded ...

django django-admin
Rectangle 27 0

Django doesn't have a built in way of doing this that I'm aware of but I can think of a couple of ways you could do something once, rather than having to manually modify lots of fieldsets.

One approach would be to use javascript to rewrite the page markup. Maybe the javascript could have a list of fieldnames and whenever it finds one of those it hides the field and it's label and adds a button to the page to toggle these invisible fields.

The other approach would just involve python. Normally you just specify the fieldsets attribute in the admin as a tuple. But you could specify it as an imported function which takes the usual tuple as an argument. In your settings file you could specify a list of fieldnames you want to hide. You then need to write a function that returns a modified tuple, moving any fields that match one of your fieldnames into a new fieldset along with the collapse class.

fieldsets = hide_fields(
    (None,
        {'fields':('title', 'content')}
    )
)

This might end up being interpreted as the following, assuming content is in the settings file as something you want to hide:

fieldsets = (
    (None,
        {'fields':('title',)}
    ),
    ('Extra',
        {
            'fields':  ('content',),
            'classes':('collapse',),
        }
    ),
)

python - How To Collapse Just One Field in Django Admin? - Stack Overf...

python django django-admin
Rectangle 27 0

Yes, I am aware of this, and I discuss it in the first paragraph of my question.

python - How To Collapse Just One Field in Django Admin? - Stack Overf...

python django django-admin
Rectangle 27 0

class PageAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('title', 'content', )
        }),
        ('Other Informations', {
            'classes': ('collapse', 'open'),
            'fields': ('slug', 'create-date',)
        }),
    )
{% extends "admin/model_name/change_form.html" %}

{% block extrahead %}
    {{ block.super }}
    <script src="{{ STATIC_URL }}admin/js/collapse-open.js" type="text/javascript"></script>
{% endblock %}
(function($) {
    $(document).ready(function() {
        $('fieldset.collapse.open').removeClass('collapsed');
    });
})(django.jQuery);

Django Admin - add collapse to a fieldset, but have it start expanded ...

django django-admin
Rectangle 27 0

Thanks, I'll probably end up going with that. We'd used grappelli on a couple other projects, and I knew it could do this, just didn't think of it at the time.

Django Admin - add collapse to a fieldset, but have it start expanded ...

django django-admin
Rectangle 27 0

Old question, but I ran into the same one and came up with an answer which can be implemented using standard django:

create file: admin/(app)/(model)/change_form.html in your templates directory to make your (model) of your (app) use that form file.

{% extends "admin/change_form.html" %}

{% block extrahead %}
    <!-- Load superblock (where django.jquery resides) -->
    {{ block.super }}
    <!-- only do this on 'add' actions (and not 'change' actions) -->
    {% if add and adminform %}
    <script type="text/javascript">
        (function($) {
            $(document).ready(function($) {
                //Remove the 'collapsed' class to make the fieldset open
                $('.collapse').removeClass('collapsed');

                //Remove the show/hide links
                $('.collapse h2 a').remove();

                //Tidy up by removing the parenthesis from all headings
                var $headings = $(".collapse h2");
                $headings.each(function(i, current){
                    var str = $(current).text();
                    parenthesisStart = str.indexOf('(');
                    $(current).text(str.substring(0, parenthesisStart));
                });
            });                                 
        })(django.jQuery);                      
    </script>
    {% endif %}
{% endblock %}

Django Admin - add collapse to a fieldset, but have it start expanded ...

django django-admin