开发者

How to hide the label for the hidden field

I have been trying to hide the label of the hidden field defined in a model class using Django api. The CRUD operations of the model are managed by Django admin.

My model is

class RackForm(django.forms.ModelForm):

  def __init__(self, *args, **kwargs):
     self.fields['racktypeid'].widget = \
         forms.HiddenInput(attrs={'styles': 'display:none;'})
     ..... other initialization

The generated html source is given below

<div class="form-row racktypeid">


            <div>

                    **<label for="id_racktypeid" class="required">Rack Type:</label>**

                        **<input styles="display:none;" name="racktypeid" value="3" onchange="changeRackType(t开发者_高级运维his);" type="hidden" id="id_racktypeid" />**




                    <p class="help">The physical type of rack as defined in the rack type list</p>

            </div>

    </div>

The field has been hidden but not the associated label. I have even tried using jQuery for it. I may be wrong with the syntax. If I am, please correct me if I am wrong.

$(document).ready(function() {
    // Hide label for RackForm rack type id label.
   $('#id_racktypeid, label[for="#id_racktypeid"]').hide()
    $('#id_racktypeid, label[for="#id_racktypeid"]').parent().hide()
  });
});

None of the options succeed. Is there anything I need to do hide the label of the hidden field?

Update: Eventually I chose to hide the entire div tag.


Your problem stems from using {{ form.as_p }} or some other other shortcut function to render your form. Render the form yourself and you won't have this problem.

Also, in Django 1.3 there is an easier way to change the widget for a field, see http://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜