[英]“TypeError: string indices must be integers” when using cleaned_data
The error occurs here: 错误发生在这里:
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
clean = form.cleaned_data
username = clean['username']
email = clean['email']
password = clean['password']
new_user = User.objects.create_user(username, email, password)
new_user.save()
new_account = Account(user=new_user, email=email)
new_account.save()
At the username = clean['username']
line. 在
username = clean['username']
行。 I've been able to use this exact line successfully in other places without issue. 我已经能够在其他地方成功使用此确切的路线而没有问题。 Why is it an issue now?
为什么现在是一个问题?
您可能从表单的clean()
方法返回了错误的内容-您应该返回完整的self.cleaned_data
字典。
Apparently cleaned_data
is giving you a string, not a dictionary. 显然
cleaned_data
给您一个字符串,而不是字典。
As a string can only be indexed by numbers, it's giving you this error. 由于字符串只能由数字索引,因此会出现此错误。
Try printing the value to see what is going on. 尝试打印该值以查看发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.