1
我無法在用戶提交表單時獲取first_name和last_name變量進行保存。這是我的代碼,我需要幫助。姓氏和姓氏未保存在自定義Django_Registration表單中
Models.py
from django.db import models
from django.contrib.auth.models import User
class MyRegistration(models.Model):
first_name = models.CharField(max_length = 100, null = True, unique = True)
last_name = models.CharField(max_length = 100, null = True, unique = True)
def __unicode__(self):
return self.name
Forms.py
from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import MyRegistration
from django import forms
class MyRegistrationForm(forms.ModelForm):
class Meta:
model = MyRegistration
RegistrationForm.base_fields.update(MyRegistrationForm.base_fields)
class CustomRegistrationForm(RegistrationForm):
def save(self, profile_callback = None):
user = super(CustomRegistrationForm, self).save(profile_callback = None)
org, c = MyRegistration.objects.get_or_create(user=user, first_name =
self.cleaned_data['first_name'], last_name = self.cleaned_data['last_name'])
在根urls.py
url(r'^accounts/register/$', 'registration.views.register', {'form_class':
CustomRegistrationForm, 'backend':'registration.backends.default.DefaultBackend'}),