Always including the user in the django template context

前端 未结 8 645
一向
一向 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:28

    The hints are in every answer, but once again, from "scratch", for newbies:

    authentication data is in templates (almost) by default -- with a small trick:

    in views.py:

    from django.template import RequestContext
    ...
    def index(request):
        return render_to_response('index.html', 
                                  {'var': 'value'},
                                  context_instance=RequestContext(request))
    

    in index.html:

    ...
    Hi, {{ user.username }}
    var: {{ value }}
    ... 
    

    From here: https://docs.djangoproject.com/en/1.4/topics/auth/#authentication-data-in-templates

    This template context variable is not available if a RequestContext is not being used.

提交回复
热议问题