Creating a Django Registration Form by Extending Django-Registation Application

女生的网名这么多〃 提交于 2019-12-05 02:45:13

问题


Am trying to create a registration form by extending django-registration app and using Django Profile. I have created the model and form for the profile and when I checked through the django shell it is generating the fields. For the profile fields i am using ModelForm. Now I am struck on how to bring both the django-registration and the profile fields together. Following are the code i have developed

model.py

class UserProfile(models.Model):
    """
    This class would define the extra fields that is required for a user who will be registring to the site. This model will
    be used for the Django Profile Application
    """
    GENDER_CHOICES = ( 
        ('M', 'Male'),
        ('F', 'Female'),
    )

    #Links to the user model and will have one to one relationship
    user = models.OneToOneField(User)    

    #Other fields thats required for the registration
    first_name = models.CharField(_('First Name'), max_length = 50, null = False)    
    last_field = models.CharField(_('Last Name'),max_length = 50)
    gender = models.CharField(_('Gender'), max_length = 1, choices=GENDER_CHOICES, null = False)    
    dob = models.DateField(_('Date of Birth'), null = False)    
    country = models.OneToOneField(Country)
    user_type = models.OneToOneField(UserType)
    address1 = models.CharField(_('Street Address Line 1'), max_length = 250, null = False)
    address2 = models.CharField(_('Street Address Line 2'), max_length = 250)
    city = models.CharField(_('City'), max_length = 100, null = False)
    state = models.CharField(_('State/Province'), max_length = 250, null = False)
    pincode = models.CharField(_('Pincode'), max_length = 15)
    created_on = models.DateTimeField()
    updated_on = models.DateTimeField(auto_now=True)

forms.py

class UserRegistrationForm(RegistrationForm, ModelForm):

    #resolves the metaclass conflict
    __metaclass__ = classmaker()

    class Meta:
        model = UserProfile
        fields = ('first_name', 'last_field', 'gender', 'dob', 'country', 'user_type', 'address1', 'address2', 'city', 'state', 'pincode')

Now what should i do to mix django-registration app with my custom app. I had gone through lots of sites and links to figure it out including Django-Registration & Django-Profile, using your own custom form but i am not sure to move forward especially since i am using ModelForm instead.

UPDATE (26th Sept, 2011)

I made the changes as suggested by @VascoP below. I updated template file and then from my view.py i created the following code

def register(request):
    if request.method == 'POST':        
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            UserRegistrationForm.save()
    else:
        form = UserRegistrationForm()
    return render_to_response('registration/registration_form.html',{'form' : form}, context_instance=RequestContext(request))

After the following change the form is correctly getting rendered but the problem is that the data is not getting saved. Please help me.

UPDATE (27th Sept, 2011)

UserRegistrationForm.save() was changed to form.save(). The updated code is for views.py is as follows

def register(request):
    if request.method == 'POST':        
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = UserRegistrationForm()
    return render_to_response('registration/registration_form.html',{'form' : form}, context_instance=RequestContext(request))

Even after the update the user is not getting saved. Instead I am getting an error

'super' object has no attribute 'save'

I can see that there is no save method in RegistrationForm class. So what should i do now to save the data? Please help


回答1:


You are aware that the User model already has first_name and last_name fields, right? Also, you misstyped last_name to last_field.

I would advise to extend the form provided by django-registration and making a new form that adds your new fields. You can also save the first name and last name directly to the User model.

#get the form from django-registration
from registration.forms import RegistrationForm

class MyRegistrationForm(RegistrationForm):
    first_name = models.CharField(max_length = 50, label=u'First Name')    
    last_field = models.CharField(max_length = 50, label=u'Last Name')
    ...
    pincode = models.CharField(max_length = 15, label=u'Pincode')


    def save(self, *args, **kwargs):
        new_user = super(MyRegistrationForm, self).save(*args, **kwargs)

        #put them on the User model instead of the profile and save the user
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.save()

        #get the profile fields information
        gender = self.cleaned_data['gender']
        ...
        pincode = self.cleaned_data['pincode']

        #create a new profile for this user with his information
        UserProfile(user = new_user, gender = gender, ..., pincode = pincode).save()

        #return the User model
        return new_user


来源:https://stackoverflow.com/questions/7447522/creating-a-django-registration-form-by-extending-django-registation-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!