所以我正在使用我的django应用程序中的表单,事实是,它根本不会验证。当我提交它而不填写字段时,它会重定向,即使它有错误的数据,它仍然会重定向, 这是我的表格,
class CheckoutForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(CheckoutForm, self).__init__(*args, **kwargs)
#overide default attributes
for field in self.fields:
self.fields[field].widget.attrs['size'] = '30'
self.fields['credit_card_type'].widget.attrs['size'] ='1'
self.fields['credit_card_expire_year'].widget.attrs['size'] = '1'
self.fields['credit_card_expire_month'].widget.attrs['size'] = '1'
self.fields['credit_card_cvv'].widget.attrs['size'] = '5'
class Meta:
model = Orders
exclude = ('status','transaction_id',)
credit_card_number = forms.CharField()
credit_card_type = forms.CharField(widget=forms.Select(choices=CARD_TYPES))
credit_card_expire_month = forms.CharField(widget=forms.Select(choices=cc_expire_months()))
credit_card_expire_year = forms.CharField(widget=forms.Select(choices=cc_expire_years()))
credit_card_cvv = forms.CharField()
以下是应该验证表单的视图,
def show_checkout(request):
if order.is_empty(request):
cart_url = urlresolvers.reverse('order_index')
return HttpResponseRedirect(cart_url)
if request.method == 'POST':
postdata = request.POST.copy()
form = forms.CheckoutForm(request.POST,postdata)
if form.is_valid():
response = checkout.process(request)
order_number = response.get('order_number',0)
error_message = response.get('message','')
if order_number:
request.session['order_number'] = order_number
if postdata['submit'] == 'place order':
reciept_url = urlresolvers.reverse('checkout_reciept')
return HttpResponseRedirect(reciept_url)
else:
error_message = u'Correct these errors'
else:
form = forms.CheckoutForm()
context = {
'form':form,
'error_message':error_message
}
return render_to_response('checkout/checkout.html',context,context_instance=RequestContext(request))
以下是模板中的内容,
{% block content %}
<h1>Checkout</h1>{% if error_message %}
<ul class="errorlist">
<li>{{ error_message }}</li>
</ul>
{% endif %}
<form action="." method = "post">
<fieldset class="checkout">
<legend>Contact Info</legend>
<table>
{% form_table_row form.email %}
{% form_table_row form.phone %}
</table>
</fieldset>
<fieldset class="checkout">
<legend>Delivery Info</legend>
<table>
{% form_table_row form.delivery_name%}
{% form_table_row form.delivery_address %}
{% form_table_row form.delivery_city %}
</table>
</fieldset>
<fieldset class="checkout">
<legend>Billing Info</legend>
<table>
{% form_table_row form.billing_name %}
{% form_table_row form.billing_address %}
{% form_table_row form.billing_city %}
</table>
</fieldset>
<fieldset class="checkout">
<legend>Credit Card Info</legend>
<table>
{% form_table_row form.credit_card_number %}
{% form_table_row form.credit_card_type %}
{% form_table_row form.credit_card_expire_month %}
{% form_table_row form.credit_card_expire_year %}
{% form_table_row form.credit_card_cvv %}
</table>
</fieldset>
<table>
<tr>
<form method="post" action=".">
{% csrf_token %}
<th colspan="2">
<input name="submit" type="submit" value="place order" class="submit" />
</th>
</form>
</tr>
</table>
</form>
现在我知道这看起来很多,但是因为我不确定问题出在哪里,因为我从来没有碰到它,也不清楚我已经检查过的几个解释。