django allauth登录&主页上注册表 [英] django allauth login & signup form on homepage

查看:148
本文介绍了django allauth登录&主页上注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案一段时间,我无法围绕这个问题。所有我想要完成的是在同一页面和主页上,而不是在/ accounts url下面登录和注册allauth。有没有人有这方面的经验或有解决方案,他们可以分享或指向我正确的方向?任何帮助将不胜感激。

I have been looking for a solution for some time now and I am not able to wrap my head around this. All I am trying to accomplish is to have the allauth login and signup form on the same page and on the homepage instead of under the /accounts urls. Does anyone have experience with that or has a solution they could share or point me in the right direction? Any help would be appreciated.

推荐答案

首先我们使用allauth的注册视图创建自定义视图

First we take create a custom view using allauth's signup view

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


class CustomSignupView(SignupView):
    # here we add some context to the already existing context
    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

验证错误不会在这里用于登录表单,我们然后需要创建一个自定义的LoginView,但现在让我们继续模板

Validation errors will not be rendered here for login form, we then need to create a custom LoginView, but for now let's move on to the template

<button id="toggleForms">Toggle Forms</button> 
<form method='post' action='{% url 'yourviewurl %}' id='signup'>
  {% csrf_token %}
  {{ form.as_p }}
  <input type='submit' value='Sign Up'>
</form>

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

添加一些javascript来切换这些。
动作将表单指向不同的方向。通常我们将使用formets,因为All-auth的注册表单不是Form对象,这可能是最快的方法。

Add some javascript to toggle these. The actions point the forms in different directions. Normally we would use formsets for this but since All-auth's signup form is not a Form object this may be the quickest way to do it.

这些都是视图。任何你选择的应用程序的py,标签进入在settings.py,TEMPLATE_DIRS或django1.8中的Dirs列表中定义的模板

These all go in views.py of any app you choose, the tags go inside of a template defined in settings.py, TEMPLATE_DIRS or Dirs list in django1.8

这篇关于django allauth登录&amp;主页上注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆