简体   繁体   English

使用cleaned_data时出现“ TypeError:字符串索引必须为整数”

[英]“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.

相关问题 类型错误:使用带有字符串参数的 itemgetter 时,字符串索引必须是整数 - TypeError: string indices must be integers when using itemgetter with string argument 类型错误:从 json 读取数据时,字符串索引必须是整数 - TypeError: string indices must be integers when reading data from json TypeError:在 python 中使用 json 文件时,字符串索引必须是整数 - TypeError: string indices must be integers when using json file in python 使用Twython时出现“ TypeError:字符串索引必须为整数” - “TypeError: String Indices must be integers” when using Twython TypeError:尝试使用Python从API提取数据时,字符串索引必须为整数 - TypeError: string indices must be integers when trying to extract data from an API using Python 使用 Pandas Datareader 从 Yahoo Finance 获取股票数据时出现“TypeError: string indices must be integers” - "TypeError: string indices must be integers" when getting data of a stock from Yahoo Finance using Pandas Datareader TypeError:字符串索引必须是整数 - TypeError : string indices must be integers 类型错误:字符串索引必须是整数 - TypeError:string indices must be integers / string索引处的TypeError必须是整数 - TypeError at / string indices must be integers TypeError:字符串索引必须是整数 - TypeError: string indices must be integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM