简体   繁体   中英

“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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM