在下面的示例中,我試圖獲取所有要在模板中顯示的錯誤消息。我已經嘗試了所有論壇中提到的幾乎所有內容。以下是我的代碼。請告訴我,我錯過了什麼Django窗體在POST後不顯示
表
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'password1',
'password2'
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
查看
def register (request):
print(request.method)
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
print("Saved")
return render(request, 'main/product.html',{})
else:
print("Some Error", form.errors)
args = {'form':form,'carousel':'display:none'}
print(args['form'])
return render(request, 'accounts/register.html',args)
# if a GET (or any other method) we'll create a blank form
else:
form = RegistrationForm()
args = {'form':form,'carousel':'display:none'}
return render(request, 'accounts/register.html',args)
HTML模板
<form id="register" method="post" action="{% url 'register' %}">
{% csrf_token %}
<div class="form-register-with-email">
<div class="form-white-background">
<div class="form-title-row">
<h1>Create an account</h1>
</div>
{{ form.errors }}
{% for field in form %}
<div class="form-row">
<label>
<span>{{field.label}}</span>
{{ field }}
</label>
</div>
{% endfor %}
<div class="form-row">
<button type="submit">Register</button>
</div>
</div>
</div>
</form>
在我按下submit之後觸發視圖,當我在render語句之前打印時,表單顯示所有錯誤信息。但提交後,渲染實際上並未觸發屏幕上的任何更改。
下面是印刷中的django輸出。
<ul class="errorlist"><li>username<ul class="errorlist"><li>A user with that username already exists.</li></ul></li><li>password2<ul class="errorlist"><li>The password is too similar to the username.</li><li>This password is too short. It must contain at least 8 characters.</li><li>This password is too common.</li></ul></li></ul>
<tr><th><label for="id_username">Username:</label></th><td><ul class="errorlist"><li>A user with that username already exists.</li></ul><input autofocus="" id="id_username" maxlength="150" name="username" type="text" value="test" required /><br /><span class="helptext">Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</span></td></tr>
<tr><th><label for="id_first_name">First name:</label></th><td><input id="id_first_name" maxlength="30" name="first_name" type="text" value="test" /></td></tr>
<tr><th><label for="id_last_name">Last name:</label></th><td><input id="id_last_name" maxlength="30" name="last_name" type="text" value="test" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input id="id_email" name="email" type="email" value="[email protected]" required /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" required /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><ul class="errorlist"><li>The password is too similar to the username.</li><li>This password is too short. It must contain at least 8 characters.</li><li>This password is too common.</li></ul><input id="id_password2" name="password2" type="password" required /><br /><span class="helptext">Enter the same password as before, for verification.</span></td></tr>
[26/Aug/2017 10:01:19] "POST /register/ HTTP/1.1" 200 5732
我在想什麼?
在上面的代碼中,如果窗體無效,窗體將不會呈現任何內容。如果用戶輸入錯誤的數據,我希望模板顯示所有錯誤。 – sdev