我想以文本格式检索错误并将密钥,错误字典传递给表单,但它表明form.errors是不可迭代的
def signup(request):
forms={}
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('/')
else:
if form.errors:
for key,value in form.errors.items:
forms[key]=BeautifulSoup(value,'html.parser').get_text()
else:
form = SignUpForm()
return render(request, 'signup.html',{'forms':forms})
在模板中是:
<script>
{% for key,value in forms.items %}
$(document).ready(function(){
$('input[name={{key}}]').parent().addClass("alert-validate");
$('input[name={{key}}]').parent().attr("data-validate", "{{value}}");
});
{% endfor %}
<script>
给出
错误for key,value in form.errors.items:
答案 0 :(得分:0)
应该为form.errors.items()
,而不是form.errors.items
...
if form.errors:
for key,value in form.errors.items():
forms[key]=BeautifulSoup(value,'html.parser').get_text()
...