編輯:我確定了原因,因爲隨RHEL 7提供的python包。解決方案是安裝替代版本。就我而言,我只是將服務器移到了centOS,其中提供的python與django一起工作。爲什麼我的django表單在我不打電話時驗證is_valid
我有一個表單,它對我的home view有動作,它包含兩個按鈕,保存和取消。當在本地開發服務器(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服務器上運行時都沒有。
這是我的表格。注意我也嘗試刪除類:表單控件,並沒有什麼區別。我有另一種形式和視圖,行爲幾乎相同(取消處理與其他,表單是模型形式),唯一的區別是沒有日期字段。爲了排除這個問題,我排除了日期字段,但仍然存在相同的問題。
#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'})}
請張貼您的追溯。 – itzMEonTV
沒有回溯,代碼正常執行。 – Edyoucaterself
你的表格是怎樣的? – yedpodtrzitko