当我没有调用is_valid时,为什么我的django表单会验证

时间:2016-09-26 21:06:56

标签: python django python-2.7 django-1.9

编辑:我已将原因确定为RHEL 7提供的python包。解决方案是安装备用版本。在我的例子中,我只是将服务器移动到centOS,其中提供的python与django一起工作。

我有一个表单,它对我的​​家庭视图采取行动,它包含两个按钮,保存和取消。在本地开发服务器(manage.py runserver)上运行时,这可以正常工作。当我将其推送到生产时,取消按钮返回表单验证错误,尽管没有调用is_valid方法。

以下是观点:

def home(request):
#uses home.html
if request.method == 'POST':
    #Figure out which button was pressed
    #Cancel Button - Back to home
    if request.POST.get("cancel"):
        #return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        footer = request
        lineitems = Budget.build(request.user)
        c = {'lineitems': lineitems,
             'footer':footer,}
        return render(request, 'home.html', c)
    #Save button on config.html IncomeForm/Expenses Form
    if request.POST.get("config_save"):
        #ExpensesForm submitted
        if 'expenseName' in request.POST:
            form = ExpensesForm(request.POST)
            if form.is_valid():
                form.save()
            else:
                temp = 'config.html'     
                footer = 'Expense Form Invalid'
                c = {'form':form,
                     'footer':footer,}
                return render(request, temp, c)
        #IncomeForm submitted
        else:
            form = IncomeForm(request.POST)
            if form.is_valid():
                form.save()
            else:
                form = IncomeForm(request.POST)
                temp = 'config.html'     
                footer = 'Form Invalid'
                c = {'form':form,
                     'footer':footer,}
                return render(request, temp, c)
        #Use Budget Class to populate a table in template
        Budget.update_data({'months':12,
                            'user':request.user})
        temp = 'home.html'
        footer = '* Line Modified'
        lineitems = Budget.build(request.user)
        c = {'lineitems': lineitems,
             'footer':footer,}
        return render(request, temp, c)
# if a GET (or any other method) we'll load the budget
else:
    footer = '* Line item modified'
    footer = request
    Budget.update_data({'user': request.user,
                        'months':12})
    lineitems = Budget.build(request.user)
    c = {'lineitems': lineitems,
         'footer':footer,}
    return render(request, 'home.html', c)

以下是模板:

{% extends "base.html" %}
{% load bootstrap3 %}

{% block title %}
<h1>Add {{ itemtype }}</h1>
{% endblock %}

{% block content %}
<form action="{% url 'home' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="btn-group">
<input type="submit" name="config_save" value="Save" class="btn btn-primary"/>
<input type="submit" name="cancel" value="Cancel" class="btn btn-default"/>
</div>
</form>
{% endblock %}
{% block footer %}
{{ footer }}
{% endblock %}

编辑*

当我替换

时,我能够在开发环境中重新创建此问题
{{ form.as_p }}

使用

{% bootstrap_form form layout='vertical' %}

但遗憾的是,当在apache / wsgi服务器上运行时,这些都不起作用。

这是我的表格。注意我也尝试删除类:form-control,它没有任何区别。我有另一种形式和视图,其行为几乎与此相同(取消是用else处理,形式是modelform),但唯一的区别是没有日期字段。为了排除问题,我排除了日期字段,但仍然存在同样的问题。

#Edit form to add/edit Expenses and Bills
class ExpensesForm(forms.ModelForm):
    class Meta:
        model = Items
        exclude = ('skiplst',)
        widgets = {'user': forms.HiddenInput(),
                   'itemType': forms.HiddenInput(),
                   'itemName': forms.TextInput(attrs={'class':'form-control',}),
                   'category': forms.Select(attrs={'class':'form-control',}),
                   'itemAmount': forms.NumberInput(attrs={'class':'form-control',}),
                   'payCycle': forms.Select(attrs={'class':'form-control',}),
                   'itemNote': forms.TextInput(attrs={'class':'form-control',}),
                   'nextDueDate': forms.DateInput(attrs={'name': 'date',
                                                         'class':'form-control'}),
                   'endDate': forms.DateInput(attrs={'name': 'date',
                                                     'class':'form-control'})}

#Edit form to add/edit Income Sources
class IncomeForm(forms.ModelForm):
    class Meta:
        model = Items
        exclude = ('category','skiplst')
        widgets = {'user': forms.HiddenInput(),
                   'itemType': forms.HiddenInput(),
                   'itemName': forms.TextInput(attrs={'class':'form-control',}),
                   'itemAmount': forms.NumberInput(attrs={'class':'form-control',}),
                   'payCycle': forms.Select(attrs={'class':'form-control',}),
                   'itemNote': forms.TextInput(attrs={'class':'form-control',}),
                   'nextDueDate': forms.DateInput(attrs={'name': 'date',
                                                         'class':'form-control'}),
                   'endDate': forms.DateInput(attrs={'name': 'date',
                                                     'class':'form-control'})}

1 个答案:

答案 0 :(得分:0)

当您的表单提交取消值,提交cancel并且您正在检查{时,看起来很难(因为格式/缩进略有偏差) {1}},以便永远不会执行逻辑。