Django注册& Django-Profile,使用您自己的自定义窗体 [英] Django-Registration & Django-Profile, using your own custom form

查看:109
本文介绍了Django注册& Django-Profile,使用您自己的自定义窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django-registration和django-profile来处理注册和配置文件。我想在注册时为用户创建个人资料。我已经创建了一个自定义注册表单,并将其添加到urls.py中,使用本教程:



http://dewful.com/?p=70



本教程中的基本想法是覆盖默认注册表单以同时创建配置文件。



forms.py - 在我的个人资料应用程序

  from django import形式
从registration.forms导入RegistrationForm
从django.utils.translation导入ugettext_lazy作为_
从profiles.models import UserProfile
从registration.models import RegistrationProfile

attrs_dict = {'class':'required'}

class UserRegistrationForm(RegistrationForm):
city = forms.CharField(widget = forms.TextInput(attrs = attrs_dict))

def save(self,profile_callback = None):
new_user = RegistrationProfile.objects.create_inactive_user(username = self.cleaned_data ['username'],
password = self.cleaned_data [ 'password1'],
email = self.cleaned_data ['email'])
new_profile = UserProfile(user = new_user,city = self.cleaned_data ['city'])
new_profile.save ()
返回new_user

在urls.py

  from profiles.forms import UserRegistrationForm 

  url(r'^ register / $',
register,
{'backend':'registration.backends.default.DefaultBackend ','form_class':UserRegistrationForm},
name ='registration_register'),

窗体被显示,我可以进入城市,但是它不保存或创建DB中的条目。

解决方案

您已经中途了 - 您已经成功构建了一个替换默认表单的自定义表单。但是,您正试图在模型窗体上使用save()方法进行自定义处理。这在django注册的旧版本中是可能的,但是我可以从您在URL conf中指定您使用v0.8的后端的事实看出。



升级指导说:


以前,用于在注册期间收集
数据的表单预计为
实现一个save()方法,
将创建新的用户帐户。
不再这样了;创建
帐户由后端处理
所以任何自定义逻辑应该是
移动到一个自定义的后端,或
连接监听器到信号
发送在注册过程中。


换句话说,表单上的save()方法现在被忽略, 0.8。您需要使用自定义后端或信号进行自定义处理。我选择创建一个自定义的后端(如果任何人已经得到这个工作的信号,请发布代码 - 我不能使它的工作方式)。您应该可以修改此内容以保存到您的自定义配置文件。


  1. 在您的应用程序中创建一个regbackend.py。

  2. 将DefaultBackend中的register()方法复制到其中。

  3. 在方法结束时,执行查询以获取相应的User实例。

  4. 将其他表单域保存到该实例中。

  5. 修改URL conf,使其指向自定义表单和自定义后端

所以URL conf是:

  url r'^ accounts / register / $',
注册,
{'后端':'accounts.regbackend.RegBackend','form_class':MM_RegistrationForm},
name ='registration_register'
),

regbackend.py有必要的导入,基本上是DefaultBackend的副本register()方法和添加:

  u = User.objects.get(username = new_user.username) 
u.first_name = kwargs ['first_name']
u.last_name = kwargs ['last_name']
u.save()
pre>

I am making use of django-registration and django-profile to handle registration and profiles. I would like to create a profile for the user at the time of registration. I have created a custom registration form, and added that to the urls.py using the tutorial on:

http://dewful.com/?p=70

The basic idea in the tutorial is to override the default registration form to create the profile at the same time.

forms.py - In my profiles app

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from profiles.models import UserProfile
from registration.models import RegistrationProfile

attrs_dict = { 'class': 'required' }

class UserRegistrationForm(RegistrationForm):
    city = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))

    def save(self, profile_callback=None):
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
        password=self.cleaned_data['password1'],
        email=self.cleaned_data['email'])
        new_profile = UserProfile(user=new_user, city=self.cleaned_data['city'])
        new_profile.save()
        return new_user

In urls.py

from profiles.forms import UserRegistrationForm

and

url(r'^register/$',
                           register,
                           {'backend': 'registration.backends.default.DefaultBackend', 'form_class' : UserRegistrationForm},
                           name='registration_register'),

The form is displayed, and i can enter in City, however it does not save or create the entry in the DB.

解决方案

You're halfway there - you've successfully built a custom form that replaces the default form. But you're attempting to do your custom processing with a save() method on your model form. That was possible in older versions of django-registration, but I can see from the fact that you specified a backend in your URL conf that you're using v0.8.

The upgrade guide says:

Previously, the form used to collect data during registration was expected to implement a save() method which would create the new user account. This is no longer the case; creating the account is handled by the backend, and so any custom logic should be moved into a custom backend, or by connecting listeners to the signals sent during the registration process.

In other words, the save() method on the form is being ignored now that you're on version 0.8. You need to do your custom processing either with a custom backend or with a signal. I chose to create a custom back-end (if anyone has gotten this working with signals, please post code - I wasn't able to get it working that way). You should be able to modify this to save to your custom profile.

  1. Create a regbackend.py in your app.
  2. Copy the register() method from DefaultBackend into it.
  3. At the end of the method, do a query to get the corresponding User instance.
  4. Save the additional form fields into that instance.
  5. Modify the URL conf so that it points to BOTH the custom form AND the custom back-end

So the URL conf is:

url(r'^accounts/register/$',
    register,
    {'backend': 'accounts.regbackend.RegBackend','form_class':MM_RegistrationForm},        
    name='registration_register'
    ),

regbackend.py has the necessary imports and is basically a copy of DefaultBackend with just the register() method, and the addition of:

    u = User.objects.get(username=new_user.username)
    u.first_name = kwargs['first_name']
    u.last_name = kwargs['last_name']
    u.save() 

这篇关于Django注册& Django-Profile,使用您自己的自定义窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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