Login and Registration on the index page using django allauth

瘦欲@ 提交于 2019-12-13 12:34:02

问题


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'

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

the following is in 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?


回答1:


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"),
...

Neither of these are part of the public API for allauth, so use them at your own risk...



来源:https://stackoverflow.com/questions/31925528/login-and-registration-on-the-index-page-using-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!