使用Django-Registration不保存的其他字段

时间:2012-01-30 15:59:23

标签: django django-registration

我已经把头发拉了几天试图获得额外的字段,如名字和姓氏,以便用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)

URL(根)的.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)),

2 个答案:

答案 0 :(得分:1)

我在尝试使用信号保存额外的东西时遇到了麻烦。

我所做的是获取django-registration的DefaultBackend,将其复制到我的项目中,并添加代码以保存额外的字段。

然后在urls.py中我告诉“^ accounts / register / $”使用我的个性化后端。

答案 1 :(得分:0)

尼斯。这没关系。我是这样做的:

def register(self, request, **kwargs):

    ......
    new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                password, site)

   # This is my additional block
    new_user.first_name = kwargs['first_name']
    new_user.last_name = kwargs['last_name']
    new_user.save()

    data = UserProfile(user=new_user)
    data.gender = kwargs['gender']
    data.address1 = kwargs['address1']
    data.address2 = kwargs['address2']
    data.city = kwargs['city']
    data.state = kwargs['state']
    data.pincode = kwargs['pincode']
    data.save()
    # end of my block. The line below, which didn't work, was 
    # actually replaced by the above code. Ugly but works

    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user