Skip to main content
 首页 » 编程设计

python之使用 cleaned_data 时为 "TypeError: string indices must be integers"

2025年05月04日18dflying

错误发生在这里:

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() 

在 username = clean['username'] 行。我已经能够在其他地方成功地使用这条确切的线而没有问题。为什么现在是个问题?

请您参考如下方法:

您可能从表单的 clean() 方法返回了错误的内容 - 您应该返回完整的 self.cleaned_data 字典。