如何根据用户选择不同地呈现Django表单? [英] How to render django form differently based on what user selects?

查看:62
本文介绍了如何根据用户选择不同地呈现Django表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的模型和表格:

I have a model and a form like this:

class MyModel(models.Model):
    param = models.CharField()
    param1 = models.CharField()
    param2 = models.CharField()

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ('param', 'param1', 'param2')

然后,我有一个具有不同值的下拉菜单,并且基于所选的值,我正在隐藏并显示MyForm的字段.现在,如果用户从下拉菜单中选择某个值,但我必须进一步采取措施,将param2渲染为CheckboxInput小部件,但在其他情况下,它应该是标准文本字段.那我该怎么办呢?

Then I have one drop down menu with different values and based on what value is selected I'm hiding and showing fields of MyForm. Now I have to take one step further and render param2 as a CheckboxInput widget if user selects a certain value from a drop down but in other cases it should be standard text field. So how would I do that?

推荐答案

您最好的猜测是,当您从下拉菜单中选择某项时触发"POST"请求.

Your best guess would be to trigger a "POST" request when you select something from your drop down menu.

该"POST"的值必须与您用来确定要输出哪个字段的值相对应.

The Value of that "POST" has to correspond your values you use to determine which field you would like to output.

现在您实际上将需要两种形式:

Now you will actually need two forms:

class MyBaseForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ('param', 'param1', 'param2')

class MyDropDownForm(MyBaseForm):
    class Meta:
        widgets = {
            'param2': Select(attrs={...}),
        }

因此您可以看到DropDownForm是从MyBaseForm派生的,以确保它具有所有相同的属性.但是,我们修改了其中一个字段的窗口小部件.

So as you can see the DropDownForm has been derived from MyBaseForm to make sure it will have all the same properties. But we have modified the widget of one of the fields.

现在,您可以更新视图.请注意,这是未经测试的Python +伪代码

Now you can update your view. Please note, this is untested Python + Pseudocode

views.py

def myFormView(request):
  if request.method == 'POST': # If the form has been submitted...    
    form = MyBaseForm(request.POST)

    #submit button has not been pressed, so the dropdown has triggered the submission. 
    #Hence we won't safe the form, but reload it            
    if 'my_real_submitbotton' not in form.data:     

      if 'param1' == "Dropdown":
         form = MyDropDownForm(request.POST)

    else:
      #do your normal form saving procedure
  else:
    form = ContactForm() # An unbound form

  return render(request, 'yourTemplate.html', {
    'form': form,
  })

此机制执行以下操作:提交表单后,它会检查您是否已按下提交"按钮或使用下拉onChange触发提交.我的解决方案不包含您需要使用onChange触发提交的javascript代码.我只想提供一种解决方法.要在form.data构造中使用"my_real_submitbutton",您将需要命名您的提交按钮:

This mechanism does the following: When the form is submitted it checks if you have pressed the "submit" button or have used a dropdown onChange to trigger a submission. My solution doesn't contain the javascript code you need to trigger the submission with an onChange. I just like to provide a way to solve it. To use the 'my_real_submitbutton' in form.data construct you will be required to name your submit button:

<input type="submit" name="my_real_submitbutton" value="Submit" />

您当然可以选择任何字符串作为Name.:-)

Of course you can choose any string as Name. :-)

如果通过下拉字段进行提交,则必须检查在此下拉菜单中选择了哪个值.如果此值满足要返回下拉菜单"的条件,则创建一个DropDownForm(request.POST)实例,否则您可以保留所有内容并重新呈现模板.

In case of a submit by your dropdown field you must check which value has been selected in this drop down menu. If this value satisfies the condition you want to return a Dropdown Menu you create an instance of DropDownForm(request.POST) otherwise you can leave everything as it is and rerender your template.

不利的是,这将刷新您的页面.从好的方面来说,它将保留所有已经输入的字段值.因此,这里没有造成伤害.

On the downside this will refresh your page. On the upside it will keep all the already entered field values. So no harm done here.

如果您希望避免刷新页面,可以保留我的建议,但是您需要通过AJAX呈现新表单.

If you would like to avoid the page refresh you can keep my proposed idea but you need to render the new form via AJAX.

这篇关于如何根据用户选择不同地呈现Django表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆