Django:如何隐藏所有表单字段?

时间:2012-09-23 14:37:58

标签: python html django forms django-forms

我有一个大约8个字段的Django表单类。如何在我的一个视图中动态隐藏所有这些表单字段?

样品:

class FormName(forms.Form):
    first_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs="class":"validate[required,first_name]","tabindex":"4"}), required=True)
    middle_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs="class":"validate[middle_name]","tabindex":"5"}), required=False)
    last_name = forms.CharField(max_length=40, widget=forms.TextInput(attrs="class":"validate[required,last_name]","tabindex":"6"}), required=True)

我想这样做的原因是因为我会在其中一个注册页面中使用相同的表单,然后在其他地方再次使用类似的表单,我希望隐藏这些表单字段。我不想创建一个单独的类,使用“widget = forms.HiddenInput()”复制相同的字段。

2 个答案:

答案 0 :(得分:5)

如果您需要带有隐藏输入的表单,您可以通过将其他变量传递给__init__方法来通知您的表单,例如:form = FormName(is_hidden=True)。您的表单可能如下所示:

class FormName(forms.Form):
    first_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs="class":"validate[required,first_name]","tabindex":"4"}), required=True)
    middle_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs="class":"validate[middle_name]","tabindex":"5"}), required=False)
    last_name = forms.CharField(max_length=40, widget=forms.TextInput(attrs="class":"validate[required,last_name]","tabindex":"6"}), required=True)

    def __init__(self, *args, **kwargs):
        is_hidden = kwargs.pop('is_hidden', None)
        super(FormName, self).__init__(*args, **kwargs)
        if is_hidden:
            self.fields['first_name'].widget = forms.HiddenInput()
            self.fields['middle_name'].widget = forms.HiddenInput()
            self.fields['last_name'].widget = forms.HiddenInput()

在所有其他情况下,您可以将表单用作ussual,而不传递is_hidden变量,它将使用您定义的默认小部件。

此外,您可以迭代self.fields字典,并隐藏所有字段小部件,而不必手动覆盖它们

答案 1 :(得分:0)

如果您认为自己会反复使用某些内容,我建议使用基于类的视图,例如:表单。您的观点应如下所示:

views.py:

class BaseView(object):

    def __init__(self, request = None , form  = None, <#define anything you want here, like model = None or template = None>):
        if form:
            self.form = form
        else:
            self.form = FormName

        if #AnythingIWant:
            self.anythingiwant = anythingiwant
        else:
            self.anythingiwant = defaultofanythingiwant

    def __call__(self, request):
        self.request = request
        if self.request.method == 'POST':
            return self.POST_handler()
        else:
            return self.GET_handler()

    def POST_handler(self):
        #method to handle post request

    def GET_handler(self):
        #method to handle get request
        #because u want specific form reusable just add:
        # context = {'form' : self.form()}
        # then render a template with the context above

    def extra_context(self):
        return {}

稍后如果您想使用该表单,只需继承BaseView,例如:

class IndexView(BaseView):
    #blahblahblah

如果您想在GET_handler()使用extra_context中添加内容,例如,如果您想添加其他表单:

views.py:

class ProfileView(BaseView):

    def extra_context(self):
        form = MyCustomForm()
        context = {'custom_form' : form}
        return context

至于urls.py:

url(r'^$', ProfileView(), name='profile_view'),

如果你想更改默认表单,只需覆盖它:

url(r'^$', ProfileView(form = MyCustomCustomForm), name='profile_view'),

现在,您可以在继承MyCustomCustomForm的每个视图中使用BaseView。使用基于类的视图的好处是,您可以对所有内容执行此操作,而不仅仅是表单,也许您想要反复使用某些模型