I have a modified Django login form and I have to address it in the template file using the long model like this {% crispy formname formname.helper %}
. I can't use the short version ({% crispy form %}
) because I have to differentiate between multiple forms. The thing is, all works well for normal forms, but not for a modified Django login form.
The code goes like this:
forms.py
from crispy_forms.helper import FormHelper
from django.contrib.auth.forms import AuthenticationForm
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'login-form'
self.helper.form_show_labels = False
self.helper.layout = Layout(
Field('username', placeholder="E-mail"),
Field('password', placeholder="Password")
)
self.helper.add_input(Submit('submit', 'Log in', css_class='btn-block btn-inset'))
views.py
from django.contrib.auth import authenticate, login as auth_login, REDIRECT_FIELD_NAME
from django.contrib.auth.views import login as django_login
from accounts.forms import LoginForm
from django.http import HttpResponseRedirect
def login(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profiles/create/')
else:
response = django_login(request, template_name='accounts/login.html', authentication_form=LoginForm)
return response
When I try to address it in the template in the form of {% crispy response response.helper %}
I only get an error stating VariableDoesNotExist at /accounts/whateverurl/: Failed lookup for key [response]
.
How should I address it?
Django 1.6
EDIT:
The solution works when I want to call the login form from that particular view, but when I try to call it from profiles/views.py, not so much.
The profiles/views.py looks like this:
from django.contrib.auth import authenticate, login as auth_login, REDIRECT_FIELD_NAME
from django.contrib.auth.views import login as django_login
from django.views.generic import DetailView
from accounts.forms import LoginForm, RegisterForm
from accounts.views import get_redirect_url
class ProfileView(DetailView):
model = Profile
def get(self, request, *args, **kwargs):
#lots of irrelevant code#
if request.user.is_authenticated():
pass
else:
login_form = django_login(request, template_name='accounts/login.html', authentication_form=LoginForm).render()
#lots of irrelevant code#
context.update({
#lots of irrelevant code#
'login_form': login_form,
})
Do I even need to update context for login_form? Anyways, using it like this I get the same VariableDoesNotExist at /profiles/whateverurl/: Failed lookup for key [form]
.
When I replace {% crispy form form.helper %}
with {% crispy login_form login_form.helper %}
I get VariableDoesNotExist at /profiles/whateverurl/: Failed lookup for key [helper]
instead.
I also tried to clone the working view into profiles/views.py and it does work, but only independently. If I include
the new login view's template into ProfileView's template, it returns the error shown above.