python - 多重注册,使用 django-allauth 的注册表

标签 python django django-allauth

我正在处理的应用程序需要为 2 种不同类型的用户单独登录。我们需要“客户”和“企业”所有者能够注册。

对于“业务”所有者,我需要做的就是将 bool 值 user.is_business 设置为 True

我已经将 ACCOUNT_SIGNUP_FORM_CLASS 与一个单独的类一起使用,该类将 bool 值设置为 true,并且效果很好。 但是随后客户端登录不再起作用。

有没有办法为不同的用户创建单独的注册 View ?

我试过以下方法

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'),

问题是不知何故, bool 值 is_business 从未设置为 True。 从显示,我可以保存,但保存的永远不是企业,永远是客户。 BusinessSignupForm 是在 allauth 表单中找到的 SignUpForm 的副本。

我做错了什么?

最佳答案

我会回答这个问题,因为我找到了使用 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>

如果您有自定义用户模型,可以反复使用它来修改自定义用户模型的属性。

当前正在运行 Django==1.8.10 和 django-allauth==0.24.1

关于python - 多重注册,使用 django-allauth 的注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875861/

相关文章:

python - 是什么导致了一个简单的函数调用导致此 KeyError?

Python - 转换错误

python - 如何从Python获取地址坐标

python - Django - 如何有效地将列表添加到多对多模型字段

django-allauth:仅允许来自特定Google Apps域的用户

python - 无效的 block 标记 - bootstrap-form,预期的 endblock

python - 当字符串索引超出范围时如何使字符串索引循环

python - 将 Django 连接到实时 Heroku Postgres 数据库

python - 如何使用allauth将范围参数设置为 strip 上的read_write?

Django,根据用户类型将用户重定向到不同页面