from django.shortcuts import render, redirectfrom django.http import HttpResponsefrom .models import Tutorialfrom django.contrib.auth.forms import UserCreationFormfrom django.contrib.auth import login, logout, authenticatefrom django.contrib.auth.models import Userdef register (request): form = UserCreationForm(request.POST) if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): user = form.save() login(request, user) return redirect("main:homepage") else: for msg in form.error_messages(): print(form.error_messages[msg]) return render(request, 'main/register.html', context={'form':form})但我仍然收到此錯(cuò)誤“‘dict’對(duì)象不可調(diào)用請(qǐng)求方法:POST 請(qǐng)求 URL: http: //127.0.0.1 :8000/register/ Django 版本:3.0.7 異常類型:TypeError 異常值:‘dict’對(duì)象不可調(diào)用"
1 回答

慕尼黑5688855
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
for?msg?in?form.error_messages(): ????print(form.error_messages[msg])
首先,Django 文檔中沒有任何地方用于form.error_messages
錯(cuò)誤消息,如果form.error_messages
是字典,則存在 python 問題 -> ,通過調(diào)用字典dict is not callable
獲取鍵列表。.keys()
根據(jù)最新的Django 文檔,從您的表單中獲取錯(cuò)誤是使用form.errors
.
for?field,?msg?in?form.errors.items(): ????print(f'Field?{key}:?{msg}')
添加回答
舉報(bào)
0/150
提交
取消