使用django-allauth创建多个注册和登录表单

8
我正在开发的应用需要为两种不同类型的用户提供单独的登录。我们需要让“客户”和“商家”都能进行注册。
对于“商家”,我只需要将布尔值user.is_business设置为True即可。
我已经使用了ACCOUNT_SIGNUP_FORM_CLASS,并使用了一个单独的类来设置布尔值为真,这个方法非常有效。但是这样一来,客户端的登录就无法正常工作了。
有没有办法为不同的用户创建单独的注册视图?
我尝试了以下方法:
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文件中:
url(r'^organiser/$', 'authentication.views.organisersignup', name='organisersignup'),

问题在于 is_business 布尔值从未设置为 True。 尽管表单显示可以保存,但保存的永远不是商家而总是客户。BusinessSignupForm 是 allauth 表单中 SignUpForm 的副本。 我做错了什么?
2个回答

8
我会回答这个问题,因为我找到了使用allauth创建多个注册表单的解决方案。
表格:
class BusinessSignupForm(SignupForm):
    def save(self, request):
        user = super(BusinessSignupForm, self).save(request)
        user.is_organizer = True
        user.save()
        return user

View

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>

这个可以反复使用来修改自定义用户模型的属性,如果你有一个的话。
当前运行的是Django==1.8.10和django-allauth==0.24.1。

为什么我的保存函数没有运行? - cristian

2

不要使用user.is_business = True来区分用户类型,考虑使用一个BusinessProfile类。如果需要的话,您可以为每个用户设置多个配置文件类型,例如PartnerProfile、ClientProfile、SupplierProfile等。每种配置文件类型都可以拥有自己的注册、登录和个人资料页面。

以下是一些替代方案:使用django-allauth进行多用户类型注册


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接