【问题标题】:Is there a way to get direct_to_template to pass RequestContext in django?有没有办法让 direct_to_template 在 django 中传递 RequestContext?
【发布时间】:2010-05-22 04:16:55
【问题描述】:

我发现自己一遍又一遍地写同样的观点。基本上是这样的:

def home_index(request):
    return render_to_response('home/index.html', RequestContext(request))

为了保持干燥的原则,我想使用generic view。我见过direct_to_template,但它传递了一个空上下文。那么我怎样才能在使用通用视图的同时仍然获得 RequestContext 的强大功能呢?

【问题讨论】:

    标签: django django-views requestcontext


    【解决方案1】:

    direct_to_template 与所有通用视图一样,已经使用了 RequestContext,因此您无需执行任何其他操作即可启用它。

    但是我不确定您是否真正要问的是是否可以传递其他上下文项 - 您可以通过在 URLconf 或包装器视图中使用 extra_context 字典参数。

    您还应该问自己为什么要创建多个视图来简单地呈现模板。如果这就是您主要做的事情,您可能会发现 Django 的内置 flatpages 应用程序比硬编码您的视图更好。

    【讨论】:

    • 原来我有一个语法错误,所以感谢您澄清RequestContext 应该在那里,因为这让我再次查看模板。你的权利,看起来我应该使用平面页面。感谢信息和帮助。
    【解决方案2】:

    我记得有同样的问题,并写了这样的东西,但是看着direct_to_template code 似乎在新版本的 django 中这个问题不再存在了。 direct_to_template 传递正确的上下文。

    【讨论】:

      猜你喜欢
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      相关资源
      最近更新 更多