Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

django allauth login & signup form on homepage

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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>

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...