Django allauth联合注册/登录视图投掷"全球名称' get_form_class'没有定义"

时间:2015-06-02 05:54:37

标签: python django django-views django-allauth

使用this question about combining Django allauth signup and login formsthis blog post about the same,我创建了一个在本地工作正常的联合注册/登录视图。但是,当我将相同的代码放到我的登台服务器上时,我收到此错误:

File "./members/views.py", line 209, in get_context_data
    context['login_form'] = get_form_class(app_settings.FORMS, 'login', self.login_form)

NameError: global name 'get_form_class' is not defined

我不确定为什么。以下是来自views.py的适用代码:

from allauth.account.views import *
from allauth.account.forms import LoginForm, SignupForm

class JointSignupLoginView(LoginView):
    form_class = SignupForm
    login_form = LoginForm        

    def get_context_data(self, **kwargs):
        context = super(JointSignupLoginView, self).get_context_data(**kwargs)
        context['login_form'] = get_form_class(app_settings.FORMS, 'login', self.login_form)
        return context

signup = JointSignupLoginView.as_view()

这是来自urls.py的适用代码:

url(r'^accounts/signup-or-login', JointSignupLoginView.as_view(template_name="account/signup_or_login.html"), name='signup_or_login'),

以下是signup_or_login.html的适用代码:

<form id="signup_form example-form" method='post' action="{% url 'account_signup' %}" id="signup">
    {% csrf_token %}
    {{ form|bootstrap }}
    {% if redirect_field_value %}
        <input type="hidden" name="{{ redirect_field_name }}"
                   value="{{ redirect_field_value }}"/>
    {% endif %}
    <div class="form-actions col-sm-6 col-sm-offset-3">
        <button class="btn btn-primary btn-block continue-button" type="submit">{% trans "Sign Up" %}</button>
    </div>
</form>
<form class="login" method='post' action="{% url 'account_login' %}">
    {% csrf_token %}
    {{ login_form|bootstrap }}
    {% if redirect_field_value %}
         <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
    {% endif %}
    <div class="form-actions col-sm-6 col-sm-offset-3">
        <button class="btn btn-primary btn-block continue-button" type="submit">{% trans "Log In" %}</button>
    </div>
    <div class="forgot-pass-container">
         <a href="{% url 'account_reset_password' %}" id="forgot_pass">Forgot Password?</a>
   </div>
</form>

我意识到JointSignupLoginView并不是从SignupView继承的,也许它应该是,所以我添加了,但错误仍然存​​在。我也试过从allauth.account.forms导入*,但这也没有用。也许app_settings.FORMS的路径不适合分期(我不知道为什么它会有所不同)?如何摆脱此错误并使此视图处于暂存状态?

1 个答案:

答案 0 :(得分:2)

在盯着Django allauth's code很长一段时间之后,我觉得get_form_class一行只是得到了我已导入的表格!

所以我刚刚将context['login_form'] = get_form_class(app_settings.FORMS, 'login', self.login_form)更改为context['login_form'] = LoginForm,这就是诀窍!