使用 django_allauth 创建两个自定义 Singin 表单

Creating Two Custom Singin Forms using django_allauth

我正在创建一个有两个用户的小系统,这两个用户都需要 singup 表格。

为了允许社交帐户和易用性,我使用了 django_allauth。但是我 运行 遇到了创建两个具有不同字段的自定义登录表单的问题。

我已经使用了多个 Whosebug 答案,但不幸的是 none 如果他们现在增加了混乱的话,他们已经提供了帮助...

我很难相信这不是一个经常出现的用例,以前肯定有人这样做过。我当前的代码有 2 个自定义注册表单、2 个自定义注册表单视图和两个应呈现表单的自定义 URL。但是他们都使用相同的表格,我不知道为什么。

任何人都可以说明情况吗?

from .models import GraduateUserProfile
from django import forms
from allauth.account.forms import SignupForm
import datetime


def year_choices():
    return [(r, r) for r in range(2015, datetime.date.today().year + 1)]


def current_year():
    return datetime.date.today().year


class GraduateUserSignupForm(SignupForm):

    def __init__(self, *args, **kwargs):
        super(GraduateUserSignupForm, self).__init__(*args, **kwargs)
        self.fields['first_name'] = forms.CharField(required=True)
        self.fields['last_name'] = forms.CharField(required=True)
        self.fields['phone_number'] = forms.CharField(required=True)
        self.fields['degree_course_name'] = forms.CharField(required=True)
        self.fields['graduation_year'] = forms.TypedChoiceField(coerce=int, choices=year_choices, initial=current_year)

    def save(self, request):
        user = super(GraduateUserSignupForm, self).save(request)
        user.first_name = self.cleaned_data.get('first_name')
        user.last_name = self.cleaned_data.get('last_name')
        user.is_graduate = True
        user.save()
        graduate = GraduateUserProfile.objects.create(user=user)
        graduate.phone_number = self.cleaned_data.get('phone_number')
        graduate.graduation_year = self.cleaned_data.get('phone_number')
        graduate.degree_course = self.cleaned_data.get('degree_course')
        graduate.save()
        return user


class CompanyUserSignupForm(SignupForm):

    def __init__(self, *args, **kwargs):
        super(CompanyUserSignupForm, self).__init__(*args, **kwargs)
        self.fields['degree_course_name'] = forms.CharField(required=True)
        self.fields['degree_course_test'] = forms.CharField(required=True)

    def save(self, request):
        user = super(CompanyUserSignupForm, self).save(request)
        return user


from .forms import CompanyUserSignupForm, GraduateUserSignupForm
from allauth.account.views import SignupView


class CompanyUserSignupView(SignupView):
    template_name = 'account/company_signup.html'
    form_class = CompanyUserSignupForm
    redirect_field_name = 'next'
    view_name = 'company_signup'
    success_url = None

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


company_signup = CompanyUserSignupView.as_view()


class GraduateUserSignupView(SignupView):
    template_name = 'account/graduate_signup.html'
    form_class = GraduateUserSignupForm
    redirect_field_name = 'next'
    view_name = 'graduate_signup'
    success_url = None

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


grad_signup = GraduateUserSignupView.as_view()
urlpatterns = [
    path('business/signup', view=company_signup, name='company_signup'),
    path('graduate/signup', view=grad_signup, name='graduate_signup'),
]


{% load i18n %}

{% block head_title %}{% trans "Signup" %}{% endblock %}

{% block content %}
<h1>{% trans "Sign Up" %}Grad Sign up</h1>

<p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p>

<form class="signup" id="company_signup_form" method="post" action="{% url 'graduate_signup' %}">
  {% csrf_token %}
  {{ form.as_p }}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <button type="submit">{% trans "Sign Up" %} &raquo;</button>
</form>

{% endblock %}
{% extends "account/base.html" %}

{% load i18n %}

{% block head_title %}{% trans "Signup" %}{% endblock %}

{% block content %}
<h1>{% trans "Sign Up" %}BUSINESS LOGIN</h1>

<p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p>

<form class="signup" id="graduate_signup_form" method="post" action="{% url 'company_signup' %}">
  {% csrf_token %}
  {{ form.as_p }}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <button type="submit">{% trans "Sign Up" %} &raquo;</button>
</form>

{% endblock %}

我刚刚弄明白了。如果您删除 'signup': 'accounts.forms.GraduateUserSignupForm', 我的表格会正确显示

编辑:几天后我发现原来的 allauth 注册视图仍然可以查看。所以我用了这段小代码

path('accounts/signup/', page_not_found, {'exception': Exception('Not Found')}, name="default_signup"),

如果有人试图查看它,抛出 404