【问题标题】:Value Error:need more than 1 value to unpack(Django)值错误:需要超过 1 个值才能解压(Django)
【发布时间】:2016-01-04 16:22:54
【问题描述】:

我看过类似的问题,但找不到任何解决方案。我的问题是当我单击注册按钮时,信息已保存并且在 django 管理中可见。但是当我点击重定向网址中的注册按钮时,我得到这个错误。 我的 forms.py 是

from django import forms
from .models import SignUp
class ContactForm(forms.Form):
   full_name=forms.CharField()
   email=forms.EmailField()
   message=forms.CharField()

class SignUpForm(forms.ModelForm):
   class Meta:
      model=SignUp
      fields=['full_name','email']
   def clean_email(self):
      email=self.cleaned_data.get("email")
      mail_base,provider=email.split("@")
      domain,extension=provider.split(".")
      if not extension=="edu":
         raise forms.ValidationError("Plase enter valid email address")
      return email

我的views.py是

from django.conf import settings
from django import forms
from django.core.mail import send_mail
from django.shortcuts import render
from .forms import ContactForm,SignUpForm

# Create your views here.
def home(request):
   title="welcome"
   form=SignUpForm(request.POST or None)
   context={
      "title":title,
      "form":form
   }
   if form.is_valid():
      instance=form.save(commit=False)
      full_name=form.cleaned_data.get("full_name")
      if not full_name:
         full_name="new"
      instance.full_name=full_name
      instance.save()
      context={
       "title":"Thank you"
       }



    return render(request,"home.html",context)


def contact(request):
  form=ContactForm(request.POST or None)
  if form.is_valid():
    email=form.cleaned_data.get("email")
    message=form.cleaned_data.get("message")
    full_name=form.cleaned_data.get("full_name")
  context={
    "form":form,
   }
  return render(request,"forms.html",context)

models.py 是

from django.db import models

# Create your models here.
class SignUp(models.Model):
   email=models.EmailField(blank=True)
   full_name=models.CharField(max_length=120,blank=False)
   timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
   updated=models.DateTimeField(auto_now_add=False,auto_now=True)

   def __unicode__(self):
     return self.email

我在 forms.py 中的 "mail_base,provider=email.split("@")" 收到错误。请帮助我..

【问题讨论】:

  • email拆分时的值是多少?
  • @JCVanHamme 普通电子邮件,如 gmail...
  • 请显示SignUp模特。
  • @VladimirDanilov 我已添加
  • 除了检查扩展名之外,您似乎在 clean 方法中做的不多 - 为什么不先做 email=self.cleaned_data.get("email", "") 然后 if not email.endswith(".edu"): raise ValidationError

标签: python django forms


【解决方案1】:

如果有人将email 字段留空(这是允许的,因为注册模型email = EmailField(blank=True)),那么self.cleaned_data.get('email') 将返回''(空字符串)。在这种情况下,mail_base, provider = email.split('@') 将引发该异常。

【讨论】:

  • @abhaygurrala 也看看 karthikr 对您帖子的评论。
【解决方案2】:

发生这种情况的原因是您的email 字符串由于某种原因没有“@”符号。这会导致 email.split("@") 生成长度为 1 的列表,而不是您期望的长度为 2 的列表。

我的猜测是您在某处有一封空白电子邮件,但任何没有“@”符号的 email 字段都会导致引发此异常。

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2013-04-04
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    相关资源
    最近更新 更多