我怎么能在我的django视图中看出,哪两个形式的字段被填充了?

时间:2016-08-30 00:16:56

标签: python django django-forms django-views

我想在网页上同时显示form1form2。如果form2已填满(if success == True),我希望我的观看不执行if form1.is_valid:

(这会产生错误)。如何在views.py?{/ p>中访问success的值

forms.py

class Form1(forms.Form):
    ....
    ....
    def clean(self):
        ....
        ....

class Form2(forms.Form):
    ....
    ....
    def clean(self):
        ....
        success = False

views.py

def my_view(request, element_pk):
    ....
    ....
    if request.method == POST:
        form2 = Form2(request.POST)
        form1 = Form1(request.POST)

    if form2.is_valid():
        ....
    if form1.is_valid():
        ....

    else:
        form2 = Form2()
        form1 = Form1()

2 个答案:

答案 0 :(得分:0)

对于您的var FExecution: IdwsProgramExecution; UnitSymbol: TUnitMainSymbol; FuncSymbol: TFuncSymbol; Info: IInfo; begin ... UnitSymbol := FExecution.Prog.UnitMains.Find('Test'); FuncSymbol := UnitSymbol.Table.FindSymbol('ClickProc', cvPublic) as TFuncSymbol; // Returns nil :-( Info := FExecution.Info.FuncBySym[FuncSymbol]; Info.Call; ... <input />标记,请为每个表单使用<button></button>参数(即。):

name

然后,在您看来,执行以下操作:

<button type="submit" name="form_1">Go!</button>

答案 1 :(得分:0)

正如您在clean方法中所做的那样设置success = False仅在本地设置它。将值存储在类(self.success = False)或cleaning_data(self.cleaned_data['success'] = False中,假设您的表单没有名为success的字段。)

然后,您可以使用以下命令访问该值:

if form2.is_valid():
    success = form2.success

if form2.is_valid():
    success = form2.cleaned_data.get('success')

有关clean功能的详细信息,请参阅docs