使用django allauth在索引页面上登录和注册 [英] Login and Registration on the index page using django allauth
问题描述
我正在使用 django-allauth
。我想要在我的网站主页上登录和注册表单,而不是在'/ accounts / login'或'/ accounts / signup'
I am using django-allauth
. I want the login and signup forms both on the home page of my website and not on '/accounts/login' or '/accounts/signup'
我创建了一个单独的应用程序以下代码位于来自allauth.account的 views.py
I have created a separate app. The following code is in 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>
当输入正确时,表单工作,表单有效。问题是当验证失败(在不正确的凭证的情况下),用户被重定向到帐户/登录并显示错误消息。如何停止此重定向并在主页上显示错误消息?
The forms work when the inputs are correct i.e. the form is valid. The problem is when the validation fails (in case of incorrect credential) the user is redirected to accounts/login and the error message is displayed. How to stop this redirect and show the error messages too on the home page?
推荐答案
重写该模板可能会更容易,以使其看起来像您的索引页。您可以指定名称(account / login.html)的模板,或者指定您自己的网址版本,其中包含不同的模板:
It would probably be easier to override the template so that it looks like your index page. You can either specify a template with the name ('account/login.html'), or specify your own version of the URL, with a different template altogether:
from allauth.account.views import LoginView
...
url(r'^my-login/$', LoginView.as_view(template_name="index.html"), name="my-login"),
...
这些都不是公共API for allauth的一部分,所以使用它们自己承担风险...
Neither of these are part of the public API for allauth, so use them at your own risk...
这篇关于使用django allauth在索引页面上登录和注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!