Always including the user in the django template context

前端 未结 8 650
一向
一向 2020-12-04 14:22

I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter

8条回答
  •  被撕碎了的回忆
    2020-12-04 15:21

    its possible by default, by doing the following steps, ensure you have added the context 'django.contrib.auth.context_processors.auth' in your settings. By default its added in settings.py, so its looks like this

    TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.auth',)
    

    And you can access user object like this,

    {% if user.is_authenticated %}
    

    Welcome, {{ user.username }}. Thanks for logging in.

    {% else %}

    Welcome, new user. Please log in.

    {% endif %}

    For more information, refer here http://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates

提交回复
热议问题