亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我想通過 django 向我的網站添加注冊。我做對了所有事情:

我想通過 django 向我的網站添加注冊。我做對了所有事情:

肥皂起泡泡 2023-03-30 16:54:55
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})但我仍然收到此錯誤“‘dict’對象不可調用請求方法:POST 請求 URL: http: //127.0.0.1 :8000/register/ Django 版本:3.0.7 異常類型:TypeError 異常值:‘dict’對象不可調用"
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

for?msg?in?form.error_messages():
????print(form.error_messages[msg])

首先,Django 文檔中沒有任何地方用于form.error_messages錯誤消息,如果form.error_messages是字典,則存在 python 問題 -> ,通過調用字典dict is not callable獲取鍵列表。.keys()

根據最新的Django 文檔,從您的表單中獲取錯誤是使用form.errors.

for?field,?msg?in?form.errors.items():
????print(f'Field?{key}:?{msg}')


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號