0

我的看法是——

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>

现在的问题是我在模板中完成的验证不再起作用,当我生成错误时,它显示视图 account.views.create_account 没有返回 HttpResponse 对象的 django 错误。而不是我想从模板中显示的错误。:( 谁能帮我这个??

4

1 回答 1

1

request.method == 'POST'如果并且form.is_valid()为 False ,您的视图将返回 None 。

取消缩进视图的最后一行以将逻辑恢复到应有的状态

于 2012-07-05T11:26:32.980 回答