Always including the user in the django template context

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

    If you can hook your authentication into the Django authentication scheme you'll be able to use request.user.

    I think this should just be a case of calling authenticate() and login() based on the contents of your Cookie.

    Edit: @Staale - I always use the locals() trick for my context so all my templates can see request and so request.user. If you're not then I guess it wouldn't be so straightforward.

    0 讨论(0)
  • 2020-12-04 15:03

    @Dave To use {{user.username}} in my templates, I will then have to use requestcontext rather than just a normal map/hash: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext

    So I guess there are no globals that the template engine checks.

    But the RequestContext has some prepopulate classes that I can look into to solve my problems. Thanks.

    0 讨论(0)
  • 2020-12-04 15:10

    @Ryan: Documentation about preprocessors is a bit small

    @Staale: Adding user to the Context every time one is calling the template in view, DRY

    Solution is to use a preprocessor

    A: In your settings add

    TEMPLATE_CONTEXT_PROCESSORS = (
        'myapp.processor_file_name.user',
    )
    

    B: In myapp/processor_file_name.py insert

    def user(request):
        if hasattr(request, 'user'):
            return {'user':request.user }
        return {}
    

    From now on you're able to use user object functionalities in your templates.

    {{ user.get_full_name }}
    
    0 讨论(0)
  • 2020-12-04 15:15

    In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own context processor.

    From the docs:

    A context processor has a very simple interface: It's just a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.

    0 讨论(0)
  • 2020-12-04 15:18

    There is no need to write a context processor for the user object if you already have the "django.core.context_processors.auth" in TEMPLATE_CONTEXT_PROCESSORS and if you're using RequestContext in your views.

    if you are using django 1.4 or latest the module has been moved to django.contrib.auth.context_processors.auth

    0 讨论(0)
  • 2020-12-04 15:18

    Use context_processors. https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TEMPLATES-OPTIONS

    settings.py

    'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'myapp.functions.test'
            ],
    },
    

    myapp/functions.py

    def test(request):
        return {'misc': 'misc'}
    
    0 讨论(0)
提交回复
热议问题