Reputation: 356
I'm using django-allauth, and trying to use one form to fill in initial data on the signup form. So, say there is a form on the home page where you fill in your email address, and that form posts to the signup form, and we just want the email address you filled in to be set as the initial data for the second form.
The way I've been trying to do it is by extending the SignupView class:
class MySignupView(SignupView):
def post(self, request, *args, **kwargs):
if not request.POST.get('submit'):
email_initial = request.POST.get("email_initial")
self.initial = {"email":email_initial}
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
else:
return super(MySignupView, self).post(self, request, *args, **kwargs)
I'm sure that the code after "if not request.POST.get('submit'):" is being run when I post to the signup form from the first form. I want email_initial to be set as the initial email on the signup form. The problem I'm having is that the second form is being validated as if it were submitted itself. This code:
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
Is exactly the same as the code that gets run on GET for the form. I'm able to set initial data when overriding the GET function, but not the POST function. Is there any way to just display the form with some initial data on POST, without validating the form?
Upvotes: 4
Views: 740
Reputation: 356
I found a solution to this problem. Instead of posting the first form to the second one, I posted to an intermediary view, saved the email address in the session, then redirected to the allauth signup form. I extended the allauth signup form to check the session for this initial email address.
The first form action is: {% url 'my_signup_email' %}
In urls.py:
url(r'^accounts/signupemail/', 'projectname.views.signup_email',name='my_signup_email'),
url(r'^accounts/signup/?', 'projectname.views.signup', name='my_signup'),
In views.py:
def signup_email(request):
request.session['email_initial'] = request.POST.get('email')
return redirect('my_signup')
class MySignupView(SignupView):
def get(self, request, *args, **kwargs):
self.initial = {"email":request.session.get('email_initial')}
return super(MySignupView, self).get(self, request, *args, **kwargs)
signup = MySignupView.as_view()
If anyone has any criticism of this solution, I would be interested to hear it.
Upvotes: 3