views:

713

answers:

4

I've got a bit of Django form code that looks like this:

class GalleryAdminForm(forms.ModelForm):
    auto_id=False
    order = forms.CharField(widget=forms.HiddenInput())

And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:

order = forms.CharField(widget=forms.HiddenInput(), label='')

I'm still left with the ":" between where the field and label used to be.

How do I hide the whole thing?!

A: 

Why do you need the field on the form at all? Just do

class GalleryAdminForm(forms.ModelForm):
    class Meta:
         exclude = ('order',)

and it will go away completely.

Daniel Roseman
I'm using custom javascript to set the value of the field based on a jQuery Sortable, so excluding the field does not work.
Gabriel Hurley
+1  A: 

If you're using JQuery this should do the trick:

Your form

TO_HIDE_ATTRS = {'class': 'hidden'}
class GalleryAdminForm(forms.ModelForm):
    auto_id=False
    order = forms.CharField(widget=forms.TextInput(attrs=TO_HIDE_ATTRS))

Javascript code to add to your template

$(document).ready(function(){
    $('tr:has(.hidden)').hide();
});

That works if you're rendering your form as a table. If you want to make it work with any kind of form rendering you can do as follows:

$(document).ready(function(){
    $('{{ form_field_container }}:has(.hidden)').hide();
});

And add form_field_container to your template context. An example:

If you render your form like this:

    <form>
        <span>{{ field.label_tag }} {{ field }}</span>
    </form>

Your context must include:

'form_field_container': 'span'

You get the idea...

Guillem Gelabert
A more generic jQuery solution would be to do `$('.hidden').parent().hide()` since that will hide the whole div that contains the widget, the label, and any help text. It's a working solution for sure. I knew about adding custom attrs to the widget, but was trying to do the whole thing without resorting to JS. I'm just surprised Django didn't have a built-in way to do this.
Gabriel Hurley
Yes,`$('.hidden').parent().hide() also works, but relies in the fact that the parent of your field also contains the label. If you had something like `<tr><td>LABEL</td><td>FIELD</td></tr>` it wont work because parent would be the TD tag not the TR one.I'm also surprised that there's no "django way" to do this.
Guillem Gelabert
+1  A: 

I think it's simpler to achieve the ":" label omission for HiddenInput widget by modifying class AdminField(object) in contrib/admin/helpers.py from :

    if self.is_checkbox:
        classes.append(u'vCheckboxLabel')
        contents = force_unicode(escape(self.field.label))
    else:
        contents = force_unicode(escape(self.field.label)) + u':'

to :

    if self.is_checkbox:
        classes.append(u'vCheckboxLabel')
        contents = force_unicode(escape(self.field.label))
    else:            
        contents = force_unicode(escape(self.field.label))
        #MODIFIED 26/10/2009
        if self.field.label <> '':
           contents += u':'
        # END MODIFY
Tonino
That's a good way to do it, though I'm not a fan of modifying anything in the django core. Could probably subclass the object and achieve the same effect that way though.
Gabriel Hurley
A: 

Another jQuery alternative that works for me in forms in django.contrib.admin (assumes you've got jQuery loaded):

$(".form-row:has(input[type=hidden])").hide();
Dominic Rodger