Django Registration Redux自定义视图未调用注册

时间:2018-06-19 16:48:59

标签: django django-registration

因此,我有一个名为“ UserProfileRegistrationForm”的自定义表单,还有一个自定义视图“ MyRegistrationView”。表单显示正确,但是输入到我的自定义字段的数据似乎没有保存。创建了User对象,但是名,姓和其余保留为空。 我的问题是在“ MyRegistrationView”中根本没有调用函数register()。有一个问题与我的here完全相同,但是我不知道他是如何解决该问题的。

非常感谢您的帮助。

View.py

class MyRegistrationView(RegistrationView):

form_class = UserProfileRegistrationForm


def register(self, request, form_class):
    new_user = super(MyRegistrationView, self).register(request, form_class)
    new_user.first_name = form_class.cleaned_data['firstName']
    new_user.last_name = form_class.cleaned_data['lastName']

    print("This is never run!!!")

    if form_class.cleaned_data['profileImage']:
        profile_image = form_class.cleaned_data['profileImage']

    location = form_class.cleaned_data['location']
    bio = form_class.cleaned_data['bio']

    user_profile = Profile.objects.create(user=new_user, profile_image=profile_image, location=location, bio=bio)

    user_profile.save()

    return new_user

Form.py

from django import forms
from registration.forms import RegistrationFormUniqueEmail
from django.contrib.auth.models import User

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    firstName = forms.CharField(label=(u'First Name'), help_text='Not required', max_length=30, required=False)
    lastName = forms.CharField(label=(u'Last Name'), help_text='Not required', max_length=30, required=False)
    profileImage = forms.ImageField(label='Profile Picture', required=False)
    location = forms.CharField(label=(u'Location'), help_text='Not required', max_length=30, required=False)
    bio = forms.CharField(label=(u'Bio'), help_text='Not required', widget=forms.Textarea, max_length=500, required=False)

    class Meta:
        model = User
        fields = ['username', 'firstName', 'lastName', 'email', 'password1', 'password2', 'profileImage', 'location', 'bio']

0 个答案:

没有答案