猿问

注册 - 序列项 0:预期的 str 实例,找到 NoneType

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)



慕田峪7331174
浏览 332回答 1
1回答

阿波罗的战车

让我简化一下您看到的错误:In [1]: ''.join([None])---------------------------------------------------------------------------TypeError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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):&nbsp; &nbsp; user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)&nbsp; &nbsp; full_address = models.CharField(null=True,blank=True,max_length=128)&nbsp; &nbsp; latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')&nbsp; &nbsp; longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')&nbsp; &nbsp; slug = models.SlugField(editable=False)&nbsp; &nbsp; def save(self, *args,**kwargs):&nbsp; &nbsp; &nbsp; &nbsp; self.slug = slugify(self.user.username)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if self.full_address:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geo_code = geocoder.geocode(self.full_address)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if geo_code:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.longitude = geo_code[0]['geometry']['lng']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.latitude&nbsp; = geo_code[0]['geometry']['lat']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; super(Profile, self).save(*args, **kwargs)
随时随地看视频慕课网APP

相关分类

Python
我要回答