gpt4 book ai didi

django - 使用 django allauth 在索引页面上登录和注册

转载 作者:行者123 更新时间:2023-12-05 07:54:02 25 4
gpt4 key购买 nike

我正在使用 django-allauth。我希望登录和注册表单都在我网站的主页上,而不是在“/accounts/login”或“/accounts/signup”上

我已经创建了一个单独的应用程序。以下代码在views.py

from allauth.account.views import SignupView
from allauth.account.forms import LoginForm


class CustomSignupView(SignupView):
# here we add some context to the already existing context
template_name = 'index.html'
def get_context_data(self, **kwargs):
# we get context data from original view
context = super(CustomSignupView,
self).get_context_data(**kwargs)
context['login_form'] = LoginForm() # add form to context
return context

下面是index.html

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>

<button id="toggleForms">Toggle Forms</button>

<form method='post' action='/accounts/signup/' id='signup'>
{% csrf_token %}
<h1>blah</h1>
{{ form.as_p }}
<input type='submit' value='Sign Up'>
</form>

<form method='post' action='/accounts/login/' id='login'>
{% csrf_token %}
{{ login_form.as_p }}
<input type='submit' value='Log In'>
</form>

</body>
</html>

表单在输入正确时工作,即表单有效。问题是当验证失败时(如果凭证不正确),用户将被重定向到帐户/登录并显示错误消息。如何停止此重定向并在主页上也显示错误消息?

最佳答案

重写模板使其看起来像您的索引页可能会更容易。您可以使用名称 ('account/login.html') 指定模板,或者使用完全不同的模板指定您自己的 URL 版本:

from allauth.account.views import LoginView

...
url(r'^my-login/$', LoginView.as_view(template_name="index.html"), name="my-login"),
...

这些都不是 allauth 的公共(public) API 的一部分,因此使用它们需要您自担风险...

关于django - 使用 django allauth 在索引页面上登录和注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com