python - Django allauth 联合注册/登录 View 抛出 "global name ' get_form_class' 未定义”

标签 python django django-views django-allauth

使用this question about combining Django allauth signup and login formsthis blog post about the same ,我创建了一个在本地运行良好的联合注册/登录 View 。然而,当我将相同的代码放入临时服务器时,我收到此错误:

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 的路径在暂存时不正确(我不知道为什么它会有所不同)?我怎样才能摆脱这个错误并让这个 View 在登台上工作?

最佳答案

盯着Django allauth's code后很长一段时间,在我看来, get_form_class 行只是获取我已经导入的表单!

所以我只是将 context['login_form'] = get_form_class(app_settings.FORMS, 'login', self.login_form) 更改为 context['login_form'] = LoginForm 这样就成功了!

关于python - Django allauth 联合注册/登录 View 抛出 "global name ' get_form_class' 未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588379/

相关文章:

html - 可以为 Django 中的扩展 html 模板定义一个单独的 css 文件吗?

python - 找不到页面 (404) 请求方法 : POST Request URL: http://127. 0.0.1:8000/accounts/signup/signup

python - 如何通过 Django 中的外键访问过滤器参数中的数据?

python - get_do_sorting_url 上的 django-admin-sortable NoReverseMatch 错误

python - 查询集在获取切片后无法重新排序查询

python - 使用列表列表清理 python 中的马虎 "for"循环

python - 将值插入列表中所有可能的位置

Django 动态 url。我究竟做错了什么?

python - 如何从 matplotlib 图中获取行数和列数?

python - 尝试在 python 中创建分组变量