Django用户注册表KeyError

时间:2016-08-11 04:27:03

标签: django keyerror

当请求空白的RegisterForm时,我得到一个KeyError。我相信当我点击网站上的链接时,我向register方法提交一个GET请求,并触发form1 = RegisterForm()行。我相信我的RegisterForm类有问题,但我无法弄清楚它是什么。我正在使用User模型和UserCreationForm。非常感谢任何帮助。

/ profile / pm /中的KeyError注册用户名'

回溯:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vagrant/project/tmp/user_profile/views.py" in register
  77.       form1 = RegisterForm()

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/forms.py" in __init__
  97.         self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': ''})

异常类型:/ profile / pm / register处的KeyError 例外值:u'用户名'

views.py

def register(request, user_type):
if user_type not in ["pm", "hm"]:
    raise Http404

if request.user.is_authenticated():
    return redirect(reverse('index'))

if user_type == 'hm':
    userTypeChoices = [Profile.RecruiterAgency, Profile.RecruiterInternal, Profile.HiringManager]
else:
    userTypeChoices = [Profile.PMEmployee, Profile.PMContractConsultant, Profile.PMBoth]

if request.method =="POST":
    # accept tos
    if 'accepted_tos' in request.POST:
        return acceptedTOS(request)

    form1 = RegisterForm(request.POST)
    form2 = ProfileForm(request.POST)
    if form1.is_valid() and form2.is_valid():
        user = form1.save()

        profile = Profile()
        profile.user_id = user.id
        profile.company = form2.cleaned_data["company"]
        profile.phone = form2.cleaned_data["phone"]
        profile.user_type = form2.cleaned_data["user_type"]
        profile.save()

        if profile.user_type in [Profile.PMEmployee, Profile.PMContractConsultant, Profile.PMBoth]:
            prospect = Prospect(profile=profile)
            prospect.save()
        else:
            employer = Employer(profile=profile)
            employer.save()

        return JsonResponse({'message':'success'})
    else:
        return JsonResponse({'message':form2.errors})
else:
    form1 = RegisterForm()
    form2 = ProfileForm()

return render(request, 'profile/register.html', {'form1':form1, 'form2':form2, 'userType':user_type, 'userTypeChoices':userTypeChoices})

forms.py

class RegisterForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    first_name = forms.CharField(label = "First name")
    last_name = forms.CharField(label = "Last name")

class Meta:
    model = User
    fields = ("email", "first_name", "last_name")

def clean_email(self):
    email = self.cleaned_data['email']
    if User.objects.filter(email=email).exists():
        raise forms.ValidationError('Email already in use.')
    return email

def save(self, commit=True):
    user = super(RegisterForm, self).save(commit=False)
    user.username = self.cleaned_data["email"]
    if commit:
        user.save()
    return user

models.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")

phone = models.CharField(max_length=200, null=True, blank=True)

RecruiterAgency = 'ra'
RecruiterInternal = 'ri'
HiringManager = 'hm'
PMEmployee = 'pe'
PMContractConsultant = 'pc'
PMBoth = 'pm'
USER_TYPE_CHOICES = (
    (RecruiterAgency, 'Recruiter - Agency'),
    (RecruiterInternal, 'Recruiter - Internal'),
    (HiringManager, 'Hiring Manager'),
    (PMEmployee, 'PM - Employee'),
    (PMContractConsultant, 'PM - Contract / Consultant'),
    (PMBoth, 'PM - Both'),
)
user_type = models.CharField(max_length=2, choices=USER_TYPE_CHOICES, default=RecruiterAgency)

company = models.CharField(max_length=200, null=True, blank=True)

bio = models.TextField(null=True, blank=True)

accepted_tos = models.BooleanField(default=False)

profile_photo = models.ImageField(null=True, blank=True, upload_to="avatar")

def __str__(self):
    return "%s's profile" % self.user.email

class Prospect(models.Model):
profile = models.OneToOneField(Profile, on_delete=models.CASCADE, null=True, blank=True, related_name="profile_prospects")
RestrictedCompany = models.ForeignKey(RestrictedCompany, on_delete=models.CASCADE, null=True, blank=True, related_name="company_prospects")
badge = models.ManyToManyField(Badge, blank=True, related_name="badge_prospects")
certification = models.ManyToManyField(Certification, blank=True, related_name="certification_prospects")

LargestProject1 = 'lp1'
LargestProject2 = 'lp2'
LargestProject3 = 'lp3'
LargestProject4 = 'lp4'
LargestProject5 = 'lp5'
LARGEST_PROJECT_CHOICES = (
    (LargestProject1, '$1-100,000'),
    (LargestProject2, '$100,001-$500,000'),
    (LargestProject3, '$500,001-$1,000,000'),
    (LargestProject4, '$1,000,001-$5,000,000'),
    (LargestProject5, '$5,000,001+'),
)
largest_project = models.CharField(max_length=3, choices=LARGEST_PROJECT_CHOICES, default=LargestProject1)

add_to_pool = models.BooleanField(default=False)

LargestTeam1 = 'lt1'
LargestTeam2 = 'lt2'
LargestTeam3 = 'lt3'
LargestTeam4 = 'lt4'
LargestTeam5 = 'lt5'
LargestTeam6 = 'lt6'
LARGEST_TEAM_CHOICES = (
    (LargestTeam1, '0-5'),
    (LargestTeam2, '6-10'),
    (LargestTeam3, '11-20'),
    (LargestTeam4, '21-50'),
    (LargestTeam5, '51-100'),
    (LargestTeam6, '100+'),
)
largest_team = models.CharField(max_length=3, choices=LARGEST_TEAM_CHOICES, default=LargestTeam1)

Contract = 'ct'
FullTimeEmployee = 'fe'
Both = 'bo'
Position_Type_CHOICES = (
    (Contract, 'Contract'),
    (FullTimeEmployee, 'Full-Time Employee'),
    (Both, 'Both'),
)
position_type = models.CharField(max_length=2, choices=Position_Type_CHOICES, default=Contract)

resume = models.FileField(null=True, blank=True, upload_to="resume")

EntryLevel = 'el'
Junior = 'jn'
Professional = 'pr'
TIER_CHOICES = (
    (EntryLevel, 'Entry-Level'),
    (Junior, 'Junior'),
    (Professional, 'Professional'),
)
tier = models.CharField(max_length=2, choices=TIER_CHOICES, default=EntryLevel)

billable_rate = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)

annual_salary = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)

total_years_exp = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)

def __str__(self):
    return "%s" % self.profile.user.email

class Employer(models.Model):
    profile = models.OneToOneField(Profile, on_delete=models.CASCADE, null=True, blank=True, related_name="profile_employers")

    title = models.CharField(max_length=200, null=True, blank=True)
    street = models.CharField(max_length=200, null=True, blank=True)
    city = models.CharField(max_length=200, null=True, blank=True)
    zip = models.CharField(max_length=200, null=True, blank=True)
    state = models.CharField(max_length=200, null=True, blank=True)
    country = models.CharField(max_length=200, null=True, blank=True)

    def __str__(self):
        return "%s" % self.profile.user.email

2 个答案:

答案 0 :(得分:0)

评论是正确的,非常有帮助!我需要将RegisterForm上的“email”字段重命名为“username”:

class RegisterForm(UserCreationForm):
username = forms.EmailField(label = "Email")
first_name = forms.CharField(label = "First name")
last_name = forms.CharField(label = "Last name")

class Meta:
    model = User
    fields = ("username", "first_name", "last_name")

def clean_email(self):
    username = self.cleaned_data['email']
    if User.objects.filter(email=email).exists():
        raise forms.ValidationError('Email already in use.')
    return username

def save(self, commit=True):
    user = super(RegisterForm, self).save(commit=False)
    user.username = self.cleaned_data["email"]
    if commit:
        user.save()
    return user

答案 1 :(得分:0)

如果UserCreationForm(在您的情况下为USERNAME_FIELD)不是表单字段,则您遇到this issue username崩溃的地方。

该问题仅影响Django 1.10,它不会影响早期版本的Django。它将在即将发布的Django 1.10.1版本中修复。