使用this question about combining Django allauth signup and login forms和 this 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/