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.
2 Answers
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
-
Thx for the help, but could you maybe also explain in which files the code should go? I found a similar approach here notesbyanerd.com/joint-login-and-signup-django-allauth-view. Looks like your solution is suggesting the same. But for some reason def get_context_data is never being called for me. It seems like I might be confusing some stuff. Would be nice if you could elaborate your solutions some more. Thx for your time!– janbrotApr 7, 2015 at 22:48
-
the views go in views.py of any app you want, the template is defined in TEMPLATES_DIRS tuple or Dirs list in django 1.8 in your settings.py.– Mikeec3Apr 8, 2015 at 2:14
-
Note for anyone trying to do this: I had to do "from allauth.account.views import *" and "from allauth.account.forms import LoginForm" instead of what is shown here. Jun 2, 2015 at 5:02
-
@Mikeec3 can you please also show us how to display the validation errors to the Login form and signup form? if I input the wrong data to the field, it will go back to the same page, but they do not show error message– JoeyNov 27, 2015 at 3:58
-
@Mikeec3 That Class view is working fine, but not able to show errors, How can be Implement for Errors, can you give some idea? anyone? May 4, 2016 at 8:15
these steps worked for me
1. goto allauth\account\views
In the loginView class under get_context_data
function add the the following code signup_form context rendering to the ret
ret.update({"signup_url": signup_url,
"site": site,
"redirect_field_name": self.redirect_field_name,
"redirect_field_value": redirect_field_value,
-->"signup_form":get_form_class(app_settings.FORMS, 'signup',SignupForm)<--
})return ret
in your app views.py
def homepage(request): template = 'account/login.html' context ={} return render(request, template, context)
your urls.py
from .views import homepage
path('', homepage, name='home'),
in your account/login.html
{% include "account/signup.html" with form=signup_form %}
see https://github.com/believeohiozua/django-allauth/blob/master/allauth/account/views.py for sample code
if you are okay with the above repository you can just install with
pip install git+https://github.com/believeohiozua/django-allauth.git