Reputation: 1139
I am using jinja2 template engine in django project. I made corresponding changes in settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [os.path.join(PROJECT_DIR.child("templates"), ''),],
'OPTIONS': {
'environment': 'web_services.settings.jinja2.environment',
}
},
]
And environment is loaded from here:
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.urlresolvers import reverse
from jinja2 import Environment
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
I now have problems with rendering django forms in the template. I have the following LoginForm:
class LoginForm(forms.Form):
email = forms.EmailField(
widget=forms.TextInput(
attrs={'class':'form-control', 'placeholder':'email',
'required':'True', 'max_length':'80'}
), label='Email address'
)
password = forms.CharField(
widget=forms.PasswordInput(
attrs={'class':'form-control', 'placeholder':'password',
'required':'True', 'max_length':'80', 'render_value':'False'}
), label="Password"
)
In templates I use this django form like this
<form method="post" name="{{ url('auth:login') }}">
<div class="form-1-field">
{{ LoginForm.email }}
{{ LoginForm.password }}
</div>
</form>
In my rendered template I get the following error {{ no such element: django.forms.forms.DeclarativeFieldsMetaclass object['email'] }}
Why is it so and how can I fix this?
EDIT
Put wrong view. Here is the one which I can't make working:
class IndexAccounts(View):
def get(self, request):
if request.user.is_authenticated():
return redirect(reverse('home:index'))
return render(request, 'login.jinja2', {'LoginForm': LoginForm})
Upvotes: 0
Views: 2866
Reputation: 15443
I assume that your LoginForm
is a ModelForm
.
Try instantiating it, you passed directly the class itself and not an instance :
class IndexAccounts(View):
def get(self, request):
if request.user.is_authenticated():
return redirect(reverse('home:index'))
return render(request, 'login.jinja2',
{'LoginForm': LoginForm()}) # <- just add the ()
Upvotes: 2