Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果我没有';无法从JSON响应中获取数据?_Python_Json_Django - Fatal编程技术网

Python 如果我没有';无法从JSON响应中获取数据?

Python 如果我没有';无法从JSON响应中获取数据?,python,json,django,Python,Json,Django,我正在创建一个应用程序,从一些用户那里获取信息,如姓名、电子邮件、ID号等。我希望从帖子中获得回复,但在某些情况下,我无法从用户那里获得电子邮件,因此我需要使用此空数据创建一个参数 换句话说,我正在对帖子的参数进行解析,并检查所有数据是否正确合法,如果我没有从API收到电子邮件,我想创建一个参数,或者至少是一个空字符串 try: if not email: errors['email'] = _(u'

我正在创建一个应用程序,从一些用户那里获取信息,如姓名、电子邮件、ID号等。我希望从帖子中获得回复,但在某些情况下,我无法从用户那里获得电子邮件,因此我需要使用此空数据创建一个参数

换句话说,我正在对帖子的参数进行解析,并检查所有数据是否正确合法,如果我没有从API收到电子邮件,我想创建一个参数,或者至少是一个空字符串

            try:
                if not email:
                    errors['email'] = _(u'This data is missing.')
                else:
                    validate_email(email)
                    if email != user_data['email']:
                        if user_data['email'] in ["", None]:
                            user_data['email'] = email
                        # If the email is modified, its no longer verified and I check if the user is registered with another email.
                        if 'email_verified' in user_data:
                            user_data['email_verified'] = False
                        users_same_mail = User.objects.filter(email=email)
                        if users_same_mail.exists():
                            # I check if the email is twice registered
                            # I give the user another email registered in the database
                            errors['email'] = _(
                                u'The email is already registered.')

            except ValidationError as e:
                errors['email'] = _(
                    u'Bad email, please enter another one.')```

What can i write to create a empty string or some value, all i want is to set email to something that is not Null because it will raise a KeyError later when i try to access that key. 
已经解决了

我通过创建if来添加密钥

如果用户_data.keys()中没有“电子邮件”:
用户数据['email']=''

如果存在任何KeyError,则通过添加此项,它将添加空字符串