0
votes

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})
1
Could you provide the view please? And the place where you call environment()? - vmonteco
@vmonteco Provided the view above. Do I need to explicitly call the environment()? As I understood from docs I just need to put reference into options in the TEMPLATES settings - AmirM
My bad, it seems you're right. Could you provide a full traceback then? - vmonteco
@vmonteco I think there is not traceback. It just renders this {{ no such element: django.forms.forms.DeclarativeFieldsMetaclass object['email'] }} and shows it on the page? I mean no standard yellow page traceback as one would expect - AmirM
Do you know which line is involved? Real question (I sincerely don't know), are you sure that your post() function only processes POST requests? - vmonteco

1 Answers

2
votes

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 ()