简体   繁体   English

如何在direct_to_template或urls.py中的简单视图中提供会话数据(不创建应用)

[英]How to provide session data in direct_to_template or simple view in urls.py (without creating apps)

I'm working on a site where many pages don't need any logic and are rendered via direct_to_template in urls.py, like so: 我在一个网站上工作,其中许多页面不需要任何逻辑,并通过urls.py中的direct_to_template呈现,如下所示:

url('^support/slp/$',
    direct_to_template, {
        'template': 'pages_fixed/support/support_slp.html',
        'extra_context': {
            'questions':  Question.objects.filter(show_slp=True).order_by('seq_num'),
            'user_state': request.session['user_state'],   # <-- tried adding, no go
        }
    },
    name='support'
),

A new requirement came up which requires me to check the state of a session var in most of these pages, to render different bits of the template. 提出了一个新要求,该要求要求我检查大多数这些页面中的会话var的状态,以呈现模板的不同位。 I tried supplying the request object but obviously, it's not available in the urls. 我尝试提供请求对象,但显然,URL中没有该对象。

I know that I can try creating a new 'app' for each such page (or, say, category of pages) and then provide the session var in each view.py view function. 我知道我可以尝试为每个此类页面(或页面类别)创建一个新的“应用”,然后在每个view.py视图函数中提供会话var。 However, this seems really excessive given how many pages there are, and I only need this one var available. 但是,考虑到有多少页,这似乎真的过高了,我只需要一个可用的var。

What would be the simplest (most light-weight) solution? 什么是最简单(最轻巧)的解决方案?

Its about time you started using the generic class based views , which will automatically pass in the request context , which will make session data available in your templates. 您开始使用基于通用类的视图的时间到了 ,该视图将自动传递到请求上下文中 ,这将使会话数据在模板中可用。

You will have to enable the template request context processor which will give you a {{ request }} variable at the template level. 您将必须启用模板请求上下文处理器 ,该处理器将在模板级别为您提供{{ request }}变量。

Here is an example using ListView : 这是使用ListView的示例:

from django.conf.urls import patterns
from django.views.generic.list import ListView

urlpatterns = patterns('',
    (r'^support/slp/$',
        ListView.as_view(template_name="pages_fixed/support/support_slp.html",
                         queryset=Question.objects.filter(show_slp=True).order_by('seq_num')),
)

In your support_slp.html template, you'll have a object_list variable that will be result of the queryset and you can use {{ request.session.user_state }} in the template. support_slp.html模板中,您将有一个object_list变量,该变量将是queryset的结果,并且可以在模板中使用{{ request.session.user_state }}

This could easily be done in a custom template tag. 这可以轻松地在自定义模板标签中完成。 Inclusion and assignment tags can optionally accept the existing context, which includes the request if you have the request context processor activated. 包含和分配标记可以选择接受现有上下文,如果您已激活请求上下文处理器,则该上下文包括请求。 You can use that to access the session. 您可以使用它来访问会话。

You could use a custom context processor to feed whatever you need into the context of all the pages. 您可以使用自定义上下文处理器将所需的任何内容馈送到所有页面的上下文中。 Suppose you put this code in package_name/context_processors.py 假设您将此代码放在package_name / context_processors.py中

def session_data(request):
    return {
        'user_state': request.session['user_state'],
    }

The you specify this context processor in TEMPLATE_CONTEXT_PROCESSORS in the settings file: 您可以在设置文件的TEMPLATE_CONTEXT_PROCESSORS中指定此上下文处理器:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'package_name.context_processors.user_data')

More documentation on context processors available here 此处提供有关上下文处理器的更多文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM