通过扩展Django注册应用程序创建Django注册表单 [英] Creating a Django Registration Form by Extending Django-Registation Application

查看:121
本文介绍了通过扩展Django注册应用程序创建Django注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过扩展django注册应用程序和使用Django配置文件来创建注册表单。我已经创建了模型和表单的配置文件,当我查看通过django shell它生成的字段。对于配置文件字段,我使用的是ModelForm。现在我很惊讶如何将django注册和配置文件字段放在一起。以下是我开发的代码



model.py

  class UserProfile(models.Model):

此类将定义要注册到该站点的用户所需的额外字段,此模型将
用于Django配置文件应用程序

GENDER_CHOICES =(
('M','Male'),
('F','女'),


#链接到用户模型,并且将有一对一的关系
user = models.OneToOneField(User)

#需要的其他字段注册
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(_('出生日期'),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 metaclass conflict
__metaclass__ = classmaker()

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

现在我应该怎么做django注册应用程序与我的自定义应用程序。我经历了许多网站和链接,以了解其中的内容,其中包括 Django注册& Django-Profile,使用您自己的自定义表单,但我不确定向前走,特别是因为我正在使用ModelForm。



更新(2011年9月26日) )



我按照下面的@VascoP的建议进行了更改。我更新了模板文件,然后从我的view.py创建了以下代码

  def注册(请求):
如果request.method =='POST':
form = UserRegistrationForm(request.POST)
如果form.is_valid():
UserRegistrationForm.save()
else:
form = UserRegistrationForm()
return render_to_response('registration / registration_form.html',{'form':form},context_instance = RequestContext(request))
pre>

以下更改后,窗体正确呈现,但问题是数据未被保存。请帮助我。



更新(2011年9月27日)



UserRegistrationForm.save()已更改为表单。保存()。更新的代码是针对views.py如下

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

即使更新后用户没有得到保存。相反,我收到一个错误


'超'对象没有属性'保存'


我可以看到在RegistrationForm类中没有保存方法。那么现在我应该怎么做来保存数据?请帮助

解决方案

你知道用户模型已经有first_name和last_name字段,对吧?此外,您将last_name错误地标记为last_field。



我建议扩展django注册提供的表单,并添加一个新表单,添加您的新字段。您也可以直接将名字和姓氏保存到用户模型。

 #从django注册$ b的表单
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)

#将它们放在User模型而不是配置文件中,并保存用户
new_user.first_name = self.cleaned_data ['first_name']
new_user.last_name = self.cleaned_data ['last_name']
new_user.save()

#get个人资料字段信息
gender = self.cleaned_data ['gender']
...
pincode = self.cleaned_data ['pincode']

#为此用户创建一个新的配置文件,其信​​息为
UserProfile(user = new_user,性别=性别,...,pincode = pincode).save()

#返回用户模型
返回new_user


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

解决方案

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

这篇关于通过扩展Django注册应用程序创建Django注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆