多次注册,使用django-allauth的注册表单 [英] Multiple signup, registration forms using django-allauth

查看:172
本文介绍了多次注册,使用django-allauth的注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用程序需要单独登录2种不同类型的用户。我们需要客户和业务所有者才能注册。

The application I am working on needs a separate login for 2 different type of users. We need "clients" and "business" owners to be able to register.

对于业务所有者,我需要做的是设置布尔值 user.is_business to True

For the "business" owner all that I need to do is set the boolean user.is_business to True

我已经使用 ACCOUNT_SIGNUP_FORM_CLASS 与一个单独的类,将布尔值设置为true,并且像一个魅力。
但是,客户端登录不再工作了。

I have used ACCOUNT_SIGNUP_FORM_CLASS with a separate class that sets the boolean to true and that works like a charm. But then the client login doesn't work anymore.

有没有办法为其他用户创建一个单独的注册视图?

Is there a way to create a separate signup view for a different user?

我尝试过以下

class BusinessUserRegistrationView(FormView):
    form_class = BusinessSignupForm
    template_name = 'allauth/account/signup.html'
    view_name = 'organisersignup'
    success_url = reverse_lazy(view_name)
organisersignup = BusinessUserRegistrationView.as_view()

表单

class BusinessSignupForm(BaseSignupForm):
    password1 = SetPasswordField(label=_("Password"))
    password2 = PasswordField(label=_("Password (again)"))
    confirmation_key = forms.CharField(max_length=40,
                                       required=False,
                                       widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):

        super(BusinessSignupForm, self).__init__(*args, **kwargs)
        if not app_settings.SIGNUP_PASSWORD_VERIFICATION:
            del self.fields["password2"]

    def clean(self):
        super(BusinessSignupForm, self).clean()
        if app_settings.SIGNUP_PASSWORD_VERIFICATION \
                and "password1" in self.cleaned_data \
                and "password2" in self.cleaned_data:
            if self.cleaned_data["password1"] \
                    != self.cleaned_data["password2"]:
                raise forms.ValidationError(_("You must type the same password"
                                              " each time."))
        return self.cleaned_data

    def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        user.is_business = True
        adapter.save_user(request, user, self)
        self.custom_signup(request, user)
        setup_user_email(request, user, [])
        return user

在urls.py



And in the urls.py

url(r'^organiser/$', 'authentication.views.organisersignup', name='organisersignup'),

问题是,不知何故,布尔is_business从不设置为True。
从显示,我可以保存,但是保存的是从来不是一个企业总是一个客户端。 BusinessSignupForm是在allauth表单中找到的SignUpForm的副本。

The problem is that somehow, the boolean is_business is never set to True. The from shows, I can save, but what is saved is never a business always a client. The BusinessSignupForm is a copy of the SignUpForm found in the allauth forms.

我做错了什么?

推荐答案

我会回答问题是我发现有多个注册表单与allauth的解决方案。

I'll answer the question as I found the solution to have multiple signup forms with allauth.

表单:

class BusinessSignupForm(SignupForm):
    def save(self, request):
        user = super(BusinessSignupForm, self).save(request)
        user.is_organizer = True
        user.save()
        return user

查看

class BusinessUserRegistrationView(SignupView):
    template_name = 'allauth/account/signup-organizer.html'
    form_class = BusinessSignupForm
    redirect_field_name = 'next'
    view_name = 'organisersignup'
    success_url = None

    def get_context_data(self, **kwargs):
        ret = super(BusinessUserRegistrationView, self).get_context_data(**kwargs)
        ret.update(self.kwargs)
        return ret

organisersignup = BusinessUserRegistrationView.as_view()

模板

 <form id="signup_form" method="post" action="{% url 'organisersignup' %}">
      {% csrf_token %}
      {% bootstrap_form form %}
 </form>

如果您有自定义用户模型的属性,则可以一遍又一遍地重复使用。

This can be reused over and over again to modify properties of the custom user model if you have one.

目前运行Django == 1.8.10和django-allauth == 0.24.1

Currently running Django==1.8.10 and django-allauth==0.24.1

这篇关于多次注册,使用django-allauth的注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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