Python 值错误:需要超过1个值才能解包(Django)
我查看过类似的问题,但找不到任何解决方案。我的问题是,当我单击“注册”按钮时,信息会被保存,并且在django administration中可见。但当我在重定向的url中单击“注册”按钮时,我会出现此错误。 我的forms.py是Python 值错误:需要超过1个值才能解包(Django),python,django,forms,Python,Django,Forms,我查看过类似的问题,但找不到任何解决方案。我的问题是,当我单击“注册”按钮时,信息会被保存,并且在django administration中可见。但当我在重定向的url中单击“注册”按钮时,我会出现此错误。 我的forms.py是 from django import forms from .models import SignUp class ContactForm(forms.Form): full_name=forms.CharField() email=forms.Emai
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
我的观点是
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
字段留空(这是允许的,因为注册模式email=EmailField(blank=True)
),那么自我清理的数据。get('email')
将返回”
(空字符串)mail\u base,provider=email.split('@')
在这种情况下将引发该异常。如果有人将email
字段留空(这是允许的,因为注册模式email=EmailField(blank=True)
),则自清理的\u数据。get('email')
将返回”
(空字符串)mail_base,provider=email。在这种情况下,split(“@”)将引发该异常。发生这种情况的原因是,由于某种原因,您的email
字符串中没有“@”符号。这将导致email.split(“@”)
生成长度为1的列表,而不是您所期望的长度为2的列表
我猜您的某个地方有一封空白电子邮件,但是任何没有'@'符号的电子邮件
字段都会引发此异常。发生这种情况的原因是您的电子邮件
字符串由于某种原因没有'@'符号。这将导致email.split(“@”)
生成长度为1的列表,而不是您所期望的长度为2的列表
我猜你的某个地方有一封空白电子邮件,但是任何没有“@”符号的email
字段都会引发此异常。当你拆分它时email
的值是多少?@JCVanHamme普通电子邮件,如gmail…ShowSignUp
model,请。@VladimirDanilov我已经添加了编辑,除了检查扩展名之外,您在clean方法中似乎做得不多-为什么不直接执行email=self.cleaned_data.get(“email”),然后执行,如果不是email.endswith(“.edu”):raise ValidationError
?当您拆分邮件时,电子邮件的价值是什么?@JCVanHamme像gmail这样的普通邮件…请显示注册
模型。@VladimirDanilov我已经添加了编辑,除了检查扩展名之外,您看起来并没有在使用clean方法做太多工作-为什么不直接做电子邮件=self.cleanned\u data.get(“电子邮件”)“,”)
,然后如果不是email.endswith(“.edu”):引发ValidationError
?@abhaygurrala也可以查看karthikr对您帖子的评论。@abhaygurrala也可以查看karthikr对您帖子的评论。