python - How to properly address a modified Django login form in the template files using crispy forms? -
i have modified django login form , have address in template file using long model {% crispy formname formname.helper %}
. can't use short version ({% crispy form %}
) because have differentiate between multiple forms. thing is, works normal forms, not modified django login form.
code goes this:
forms.py
from crispy_forms.helper import formhelper 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 auth_login, redirect_field_name django.contrib.auth.views import login django_login accounts.forms import loginform 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 try address in template in form of {% crispy response response.helper %}
error stating variabledoesnotexist @ /accounts/whateverurl/: failed lookup key [response]
.
how should address it?
django 1.6
edit:
the solution works when want call login form particular view, when try call profiles/views.py, not much.
profiles/views.py looks this:
from django.contrib.auth import authenticate, login auth_login, redirect_field_name django.contrib.auth.views import login django_login django.views.generic import detailview accounts.forms import loginform, registerform 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 need update context login_form? anyways, using same variabledoesnotexist @ /profiles/whateverurl/: failed lookup key [form]
.
when replace {% crispy form form.helper %}
{% crispy login_form login_form.helper %}
variabledoesnotexist @ /profiles/whateverurl/: failed lookup key [helper]
instead.
i tried clone working view profiles/views.py , work, independently. if include
new login view's template profileview's template, returns error shown above.
django's login view includes login form in template context form
. therefore should use:
{% crispy form form.helper %}
Comments
Post a Comment