来自模板的表单验证不起作用

时间:2012-07-05 11:22:15

标签: python django forms validation django-templates

我的观点是 -

def create_account(request):

    if request.method == 'POST':
        form = CreateAccountForm(request.POST)
        if form.is_valid():

            acc_act_date = form.cleaned_data['Account_Activation_Date']
            present_date = date.today()
            if acc_act_date <= present_date:
                stat = 'Active'
            else:
                stat = 'Inactive'

            a = Account_status.objects.get(status = stat)


            sto = Create_account_tab(account_number=form.cleaned_data['Account_Number'],
                account_name=form.cleaned_data['Account_Name'],
                account_description=form.cleaned_data['Account_Description'],
                account_status_key=a,
                account_manager=form.cleaned_data['Account_Manager'],
                parent_account_number=form.cleaned_data['Parent_Account_Number'],
                account_activation_date=form.cleaned_data['Account_Activation_Date'],
                )
            sto.save()

            return HttpResponseRedirect('/create_account/thanks/')
    else:
        form = CreateAccountForm()
        return render_to_response('CreateAccountForm.html', {'form': form}, context_instance=RequestContext(request))

我的表格是 -

class CreateAccountForm(forms.Form):
    Account_Number = forms.CharField()
    Account_Name = forms.CharField()
    Account_Description = forms.CharField()
    Account_Manager = forms.CharField()
    Parent_Account_Number = forms.CharField(required = False)
    Account_Activation_Date = forms.DateField()

我的模板是 -

<html>
<head>
    <title>Your Information</title>
</head>
<body>
    <h1>Fill in your details</h1>

{% if form.errors %}
    <p style="color: red;">
        Please correct the error{{ form.errors|pluralize }} below.
    </p>
{% endif %}

<form action="." method="post">{% csrf_token %}
    <div class="field">
        {{ form.Account_Number.errors }}
        <label for="id_Account_Number">Account Number:</label>
        {{ form.Account_Number }}
    </div>
    <div class="field">
        {{ form.Account_Name.errors }}
        <label for="id_Account_Name">Account Name:</label>
        {{ form.Account_Name }}
    </div>
    <div class="field">
        {{ form.Account_Description.errors }}
        <label for="id_Account_Description">Account Description:</label>
        {{ form.Account_Description }}
    </div>
    <div class="field">
        {{ form.Account_Manager.errors }}
        <label for="id_Account_Manager">Account Manager:</label>
        {{ form.Account_Manager }}
    </div>
    <div class="field">
        {{ form.Parent_Account_Number.errors }}
        <label for="id_Parent_Account_Number">Parent Account Number:</label>
        {{ form.Parent_Account_Number }}
    </div>
    <div class="field">
        {{ form.Account_Activation_Date.errors }}
        <label for="id_Account_Activation_Date">Account Activation Date:</label>
        {{ form.Account_Activation_Date }}
    </div>
    <input type="submit" value="Submit">
</form>

现在问题是我在模板中完成的验证不再有效,当我生成错误时,它显示django错误视图account.views.create_account没有返回HttpResponse对象。而不是我想从模板中显示的错误。 :( 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果request.method == 'POST'form.is_valid()为假,则

您的视图会返回无。

取消视图的最后一行,以恢复它应该如何的逻辑