注册 - 序列项 0:预期的 str 实例,找到 NoneType
Posted
技术标签:
【中文标题】注册 - 序列项 0:预期的 str 实例,找到 NoneType【英文标题】:Registration - sequence item 0: expected str instance, NoneType found 【发布时间】:2021-01-08 22:29:27 【问题描述】:Django 版本:3.0.8 - Python 版本:3.7.8
我最近在我的 Django 网站上发现了一个错误。当用户想要创建一个帐户时,它会返回此错误: 序列项 0:预期的 str 实例,找到 NoneType
操作:
帐户创建(检查用户名是否在数据库中不存在) 创建用户 Active = False 发送一封电子邮件以确认帐户创建。第一步出现错误,创建帐户(Active = False),未发送电子邮件并显示错误。
Traceback (most recent call last):
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/eodj89/DjangoPro/user/views.py", line 45, in signup
user.save()
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 66, in save
super().save(*args, **kwargs)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/db/models/base.py", line 795, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
for receiver in self._live_receivers(sender)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/eodj89/DjangoPro/user/models.py", line 479, in create_user_profile
UserProfile.objects.create(user=instance)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/eodj89/virtualenv/DjangoPro/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "/home/eodj89/DjangoPro/user/models.py", line 346, in save
address = " ".join([self.full_address])
Exception Type: TypeError at /account/register/
Exception Value: sequence item 0: expected str instance, NoneType found
models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
full_address = models.CharField(null=True,blank=True,max_length=128)
latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
slug = models.SlugField(editable=False)
def save(self, *args,**kwargs):
self.slug = slugify(self.user.username)
address = " ".join([self.full_address])
result = geocoder.geocode(address)
if result and len(result):
self.longitude = result[0]['geometry']['lng']
self.latitude = result[0]['geometry']['lat']
super(Profile, self).save(*args, **kwargs)
【问题讨论】:
如果 full_address 为 None 会怎样 【参考方案1】:让我简化一下你看到的错误:
In [1]: ''.join([None])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-29122f69a077> in <module>
----> 1 ''.join([None])
TypeError: sequence item 0: expected str instance, NoneType found
在你的情况下,你会得到同样的例外:
address = " ".join([self.full_address])
这意味着self.full_address
是None
。
与此同时,我认为您不需要join
,因为您尝试加入单个字符串。你对这条线有什么期望?
所以你的模型的代码看起来像:
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
full_address = models.CharField(null=True,blank=True,max_length=128)
latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
slug = models.SlugField(editable=False)
def save(self, *args,**kwargs):
self.slug = slugify(self.user.username)
if self.full_address:
geo_code = geocoder.geocode(self.full_address)
if geo_code:
self.longitude = geo_code[0]['geometry']['lng']
self.latitude = geo_code[0]['geometry']['lat']
super(Profile, self).save(*args, **kwargs)
【讨论】:
感谢您的及时德米特里。你有什么建议我避免这个错误,避免None?因为如您所知,一旦用户处于活动状态,就会使用配置文件。 我很乐意向您展示解决问题的多种方法。但我没有看到问题所在,我认为您根本不需要那个"".join(...)
- 因为没有什么可以加入的! :) 所以你可以删除那行。
还有一个关于您的要求的问题。您是否希望所有用户都必须填写“完整地址”字段?或者,也许您想让他们有机会在注册期间跳过该字段?
好的,我会删除它:) 我认为如果不是强制性的会更好。
另外,请记住,根据完整地址,您还有更多代码。如果没有地址,您也无法生成地理数据。所以你应该添加一个像if self.full_address: ...
这样的检查并继续进行这些地理计算。以上是关于注册 - 序列项 0:预期的 str 实例,找到 NoneType的主要内容,如果未能解决你的问题,请参考以下文章
mysql-connector-python cursor_cent.py 文件上的 Django 迁移错误“TypeError:序列项 1:预期类似字节的对象,str found”
从传递进来的字母序列中找到缺失的字母并返回它。 如果所有字母都在序列中,返回 undefined。