Я пару дней выдергивал свои волосы, пытаясь получить дополнительные поля, такие как имя и фамилия, чтобы сохранить их при регистрации в Django. Я получаю поля формы для отображения, но после отправки формы дополнительные поля не сохраняются. Пожалуйста, помогите мне. Спасибо!
Models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
Forms.py
from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from MyRegistration.models import UserProfile
from django.contrib.auth.models import User
class UserRegistrationForm(RegistrationForm):
first_name = forms.CharField(label=_('First Name'), max_length=30, required=True)
last_name = forms.CharField(label=_('Last Name'), max_length=30)
Regbackend.py
from django.contrib.auth.models import User
from MyRegistration.forms import UserRegistrationForm
from MyRegistration.models import UserProfile
def user_created(sender, user, request, **kwargs):
form = UserRegistrationForm(request.POST)
data = Profile(user=user)
data.first_name = form.data['first_name']
data.last_name = form.data['last_name']
data.save()
from registration.signals import user_registered
user_registered.connect(user_created, sender = UserProfile)
Urls (корень) .py
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
from MyRegistration.forms import UserRegistrationForm
from registration.views import register
import registration.backends.default.urls as regUrls
import MyRegistration.regbackend
urlpatterns = patterns('',
url(r'^accounts/register/$', register, {'backend':
'registration.backends.default.DefaultBackend','form_class': UserRegistrationForm},
name='registration_register'),
(r'^accounts/', include(regUrls)),