排除/隐藏用crispy-forms

时间:2016-05-30 07:05:46

标签: python django forms django-crispy-forms

我有两个模板具有相同的表单但在一个页面上我不想显示“照片”字段。问题是我的表格是通过FormHelper描述的脆形式。如何隐藏/排除/删除“照片”字段和FormActions(按钮)?提前谢谢。

views.py:

    def user_data_view(request):
        model_values = UserData.objects.order_by('last_name')[0]
        form = User_data_form(initial={
            'name':model_values.name,
            'last_name':model_values.last_name,
            'birth_date':model_values.birth_date,
            'bio':model_values.bio,
            'email':model_values.email,
            'jabber':model_values.jabber,
            'skype':model_values.skype,
            })
       context = {
           'form':form,
       }
       return render(request, 'home.html', context)

forms.py

class User_data_form(forms.ModelForm):
class Meta:
    model = UserData
    fields = ['name', 'last_name', 'birth_date', 'bio', 'email', 'jabber','skype', 'photo']

def __init__(self, *args, **kwargs):
    super(User_data_form, self).__init__(*args, **kwargs)

    self.helper = FormHelper()

    self.helper.form_id = 'data-fields'
    self.helper.form_method = 'POST'
    self.helper.form_tag = True
    self.helper.layout = Layout(
        Div(
            Div('name', 'last_name', 'birth_date','email', 'jabber','skype','bio', css_class='col-md-6'),
            Div('photo', css_class='col-md-6'),
            HTML('<img src="{{ url_photo }}" id="photo-preview" class="pull-right form-inline">'),

        ),
        FormActions(
        Submit('save_changes', 'Save', css_class='btn btn-success'),

        )

    )

template.html

<form id="form" action="">{% csrf_token %}
{% crispy form %}
</form>
  

更新:

我像这样重写forms.py代码:

class FormMixin(forms.ModelForm):
class Meta:
    model = UserData
    fields = ['name', 'last_name', 'birth_date', 'bio', 'email', 'jabber','skype', 'photo']

def __init__(self, *args, **kwargs):
    super(FormMixin, self).__init__(*args, **kwargs)

    self.helper = FormHelper()

    self.helper.form_id = 'data-fields'
    self.helper.form_method = 'POST'
    self.helper.form_tag = True
    self.helper.layout = Layout(
        Div(
            Div('name', 'last_name', 'birth_date','email', 'jabber','skype','bio', css_class='col-md-6'),
            Div('photo', css_class='col-md-6'),
            HTML('<img src="{{ url_photo }}" id="photo-preview" class="pull-right form-inline">'),
        ),
        FormActions()
        )

class User_data_form(FormMixin):

def __init__(self, *args, **kwargs):
    super(User_data_form, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.layout = Layout(
        Div(),
        FormActions(
        Submit('save_changes', 'Save', css_class='btn btn-success'),
        )
    )

class InitialForm(FormMixin):
class Meta:
    model = UserData
    exclude = ['photo']
def __init__(self, *args, **kwargs):
    super(InitialForm, self).__init__(*args, **kwargs)
  

但是现在我在第二个模板的表单顶部有“提交”按钮。如何重新组装代码以在表单下面放置按钮?

1 个答案:

答案 0 :(得分:0)

不是最好的决定,但不过......

在模板中你要隐藏照片,只需插入那样的smth:

 <script> document.onLoad(function(){document.getElementById('form').getElementsByTagName('img')[0].style = 'display:none';
}) </script>