我收到以下行的错误:username = login_form.cleaned_data('username'),因为在某个地方它似乎是一个字典,但我无法理解为什么。谁能告诉我,问题是什么?views.pydef index(request): return render(request, 'web/base.html')def register_view(request): register_form = UserCreationForm() if request.method == "POST": register_form = UserCreationForm(request.POST) if register_form.is_valid(): register_form.save() username = register_form.cleaned_data("username") password = register_form.cleaned_data("password1") user = authenticate(request, username=username, password=password) if user: login(request, user) return render(request, 'web/register.html', {'register_form': register_form})def login_view(request): login_form = AuthenticationForm() if request.method == 'POST': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): username = login_form.cleaned_data('username') password = login_form.cleaned_data('password') user = authenticate(request, username=username, password=password) if user: login(request, user) return render(request, 'web/login.html', {'login_form': login_form})def logout_view(request): logout(request) return redirect(reverse('web:index'))
3 回答
慕容森
TA贡献1853条经验 获得超18个赞
Form.cleaned_data
从设计上来说,它确实是一本字典。要获取字典中键的值,请使用[ ]
,例如
username = register_form.cleaned_data["username"]
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
从字典中获取值,例如:
username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password']
您无法使用 () 而是使用 [] 来获取 DICTIONARIES 中键的值。
森林海
TA贡献2011条经验 获得超2个赞
login_form.cleaned_data
是一本字典,所以username = login_form.cleaned_data('username')
没有意义,你应该使用:
username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password']
因此,元素也会发生同样的情况password
。
添加回答
举报
0/150
提交
取消