为什么我在注册Django时遇到KeyError?

时间:2018-03-24 22:11:14

标签: django

我有以下 models.py

class UserProfile(models.Model):
    GENDER_CHOICES = (
        ('m', 'Male'),
        ('f', 'Female'),
    )
    USER_TYPE = (
        ('s', 'Entrepreneur'),
        ('c', 'Mentor'),
        ('d', 'Investor'),
        ('v', 'Job Seeker'),
    )

    user = models.OneToOneField(User, on_delete = models.CASCADE)
    birth_date = models.DateField(null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    address = models.CharField(max_length=150)
    locality = models.CharField(max_length=30)
    state = models.CharField(max_length=30, default='Enter State')
    postal_code_4 = models.PositiveIntegerField(null=True)
    user_type = models.CharField(max_length=1, choices=USER_TYPE)
    skill_sets = models.CharField(max_length=150, default= 'Example:Computer Literacy/Information Technology/Business Experties')
    is_verified = models.BooleanField(default=False)

    def __str__(self):
        return self.user

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

我的views.py看起来像这样:

def Registration(request):
    if request.user.is_authenticated:
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form  = UserRegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(username = form.cleaned_data['username'],email = form.cleaned_data['email'] , password = form.cleaned_data['password'])
            user.save()
            user_profile= UserProfile(user=user, address = form.cleaned_data['address'])
            user_profile.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render(request, 'visit/registration/register.html', {'form': form},)

    else:
        form= UserRegistrationForm()
        context = {'form': form}
        return render(request, 'visit/registration/register.html', context )

我确信我在上面的代码中发现了一些错误,因为我是Django的新手。我正在尝试在注册后添加个人资料表单并面临以下错误:

KeyError at /register/
'address'
Request Method: POST
Request URL:    http://127.0.0.1:8000/register/
Django Version: 2.0.2
Exception Type: KeyError
Exception Value:    
'address'

我可以清楚地看到错误是在地址栏中,但我不知道是什么。帮助将不胜感激。 以下是我的网址的代码:

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views

app_name = 'visit'

urlpatterns = [

    url(r'^$', views.index, name='index'),
    url(r'login/', auth_views.login, {'template_name': 'visit/registration/login.html'}, name='login'),
   # url(r'accounts/profile/', views.profile, name='profile'), # need to change the app directory from here
    url(r'^register/$', views.Registration, name='registration'),
    url(r'^profile/$', views.Profile, name='profile'),
    url(r'^logout/', views.logoutRequest, name='logout'),
]

我的个人资料表格如下:

<h2>Welcome, {{ user.get_full_name }} <small>{{ user.username }}</small>!</h2>
  <p>Your birth date: {{ user.userprofile.birth_date }}</p>
     <p>Your User Type: {{ user.userprofile.user_type}}</p>
         <p>Your Gender: {{ user.userprofile.gender }}</p>
             <p>Your Address: {{ user.userprofile.address }}</p>
                 <p>Your Locality: {{ user.userprofile.locality }}</p>
                    <p>Your State: {{ user.userprofile.state }}</p>
                        <p>Your Postal code: {{ user.userprofile.postal_code_4 }}</p>
                            <p>Your Skill Sets: {{ user.userprofile.skill_sets}}</p>

0 个答案:

没有答案