Having trouble with user.is_authenticated in django template

后端 未结 2 1373
粉色の甜心
粉色の甜心 2020-12-05 17:11

Sorry if you tried helping me when I asked this earlier. Had to delete that question because I wasn\'t being allowed to edit additional information for some reason.

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 18:07

    If the auth context processor is enabled, then user is already in the template context, and you can do:

    {% if user.is_authenticated %}
    

    If you want to access request in the template, make sure you have enabled the request context processor.

    In your question you are using render_to_response. Ever since Django 1.3, it has been better to use render instead of render_to_response. Using render_to_response with RequestContext(request) works in Django <= 1.9, but from Django 1.10 onwards you must use the render shortcut if you want the context processors to work.

    return render(request, 'template/login.html', context)
    

提交回复
热议问题