开发者

django - template tag problem using AuthenticationForm

开发者 https://www.devze.com 2023-03-17 06:04 出处:网络
I am trying to put a login form in every page in my web that uses django.contrib.auth.views.login. I created a templatetag in templatetags/mytags.py, where I define a function called get_login wich lo

I am trying to put a login form in every page in my web that uses django.contrib.auth.views.login. I created a templatetag in templatetags/mytags.py, where I define a function called get_login wich looks like this:

@register.inclusion_tag('registration/login.html', takes_context=True)
def get_login(context):
    ...
    return {'formLogin':  mark_safe(AuthenticationForm())}

...and in base.html:

{% load mytags  %}{% get_login  %}

The problem now is that the template (registration/login.html) d开发者_JAVA百科oesnt recognize {{ formLogin.username }},{{ formLogin.password }}... and so on.

What am I missing?


mark_safe returns an instance of django.utils.safestring.SafeString, not a form, so those lookups will fail. I don't think there's anything wrong with directly returning the form (that's what all the generic views in django.contrib.auth do when populating templates, for instance). Just change your return statement to

return {'formLogin': AuthenticationForm()}

and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消