【问题标题】:Integrating django-profiles with django-registration将 django-profiles 与 django-registration 集成
【发布时间】:2013-05-21 16:31:02
【问题描述】:

在 django 方面我还是个新手,但我已经安装了 django-profiles 和 django-registration,并运行了最新的 django 1.5.1。我也读过一些指南,即http://dmitko.ru/django-registration-form-custom-field/http://birdhouse.org/blog/2009/06/27/django-profiles/Django-Registration & Django-Profile, using your own custom form

我没有自定义 django-registration,但我尝试制作自己的配置文件类,看起来像这样:

from django.contrib.auth.models import User
class UsrProfile(models.Model):
    user = models.ForeignKey(User, unique=True, primary_key=True)
    ...custom fields here...

但我知道可以让用户注册,但拥有个人资料是另一回事,因为用户无需创建个人资料即可注册。

我的问题是,我怎样才能坚持我制作的个人资料课程,进入注册?我想强制用户在注册时填写我在个人资料类中指定的详细信息...

我已经尝试在这里和那里遵循一些教程/指南,但每当他们说要将 urls.py 修改为以下内容时,我也会一直卡住:

url(r'^accounts/register/$',
    register,
    {'backend': ... form_class...
    ),

因为每当我尝试此操作时,似乎无法识别“寄存器”(在第二行中),我也不知道它指的是什么....我尝试类似:

from registration.views import register

但它似乎没有将注册识别为有效的导入...

【问题讨论】:

  • 它抛出的错误是什么?
  • NameError at /accounts/register name 'register' 未定义
  • 好的,我 /think/ 我已经修复了导入错误的东西,但我仍然无法弄清楚解决这一切的正确方法是什么。那个 docs.b-list.org 站点也已经关闭了很长时间。我认为我的困惑也源于以前的版本实现的东西与当前/最新版本不同的事实。我要做的就是将我已经制作的配置文件中的字段粘贴到 django-registration 的 /accounts/register/ 中。我觉得这是一个以前遇到过的微不足道的问题,但我似乎找不到太多关于它的内容。
  • 虽然我的代码中的错误已经消失,但当我导航到 /accounts/register/ 时,我得到:/accounts/register/ 处的 TypeError 必须首先使用 RegistrationView 实例调用未绑定的方法 register()参数(取而代之的是 WSGIRequest 实例)

标签: python django django-registration django-profiles


【解决方案1】:

您可以扩展类用户,并使用您想要的信息创建表单。

对于扩展用户: 模型.py

User.add_to_class('phone', models.CharField(max_length=12))
User.add_to_class('books', models.ManyToManyField(Func,null=True,blank=True))

Form.py

class AddUserForm(forms.Form):
    username = forms.CharField(label="Rut",widget=forms.TextInput())
    email    = forms.EmailField(label="Correo Electronico",widget=forms.TextInput())
    password = forms.CharField(label="Password",widget=forms.PasswordInput(render_value=False))
    phone = forms.CharField(label="Telefono",widget=forms.TextInput())
    books = forms.ModelMultipleChoiceField(queryset=Book.objects.all(),label='Books')

def clean_username(self):
    username = self.cleaned_data['username']
    try:
        u = User.objects.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError('Username is ready')

def clean_email(self):
    email = self.cleaned_data['email']
    try:
        u = User.objects.get(email=email)
    except User.DoesNotExist:
        return email
    raise forms.ValidationError('Email is ready')

view.py

def registerUser(request):
        form = AddUserForm()
    if request.method == "POST":
        form = AddUserForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            u = User.objects.create_user(username=username,email=email,password=password)
            u.phone = form.cleaned_data['phone']
            u.books = form.cleaned_data['books']
            u.save() 
            return HttpResponseRedirect('/users/')
        else:
            ctx = {'form':form}
            return      render_to_response('register.html',ctx,context_instance=RequestContext(request))

    ctx = {'form':form}
    return render_to_response('register.html',ctx,context_instance=RequestContext(request))

【讨论】:

    【解决方案2】:

    解决方案:

    在您的自定义 url.py 中

    from registration.backends.default.views import RegistrationView
    url(r'^register/$',
       RegistrationView.as_view(form_class=YourCustomForm),
       name='registration_register'),
    ),
    

    而不是:

    from registration.views import register
    url(r'^accounts/register/$',
        register,
        {'backend': ... form_class...
    ),
    

    说明:

    查看documentation

    'form_class' 参数:

    "用于用户注册的表单类。可以在每个请求的基础上被覆盖 (见>下文)。应该是实际的类对象。”

    所以“YourCustomForm”是从 RegistrationForm 类扩展而来的表单,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2013-07-12
      • 2011-02-01
      • 2011-12-15
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2011-04-15
      • 2013-10-20
      • 2011-09-19
      相关资源
      最近更新 更多