【问题标题】:Django-cms render_to_response doesn't render to templateDjango-cms render_to_response 不渲染到模板
【发布时间】:2015-10-29 19:46:00
【问题描述】:

我正在 django 1.5.12 中开发一个项目。安装了 django-cms 的版本。 我有一个由命令“pip freeze > freeze.txt”生成的文件,其中包含我已安装的下一个信息:

Django==1.5.12
MySQL-python==1.2.5
South==1.0.2
argparse==1.2.1
distribute==0.6.24
django-classy-tags==0.6.2
django-cms==2.4.3
django-mptt==0.5.2
django-sekizai==0.8.2
html5lib==1.0b7
six==1.9.0
wsgiref==0.1.2

好吧,我的问题是我的项目中有一个应用程序,我在views.py中有下一个代码:

from django.shortcuts import *
def test(request):

    data = {'test 1': [ [1, 10] ], 'test 2': [ [1, 10] ],'test 3':[ [2,20]] }
    print data                  # to see if function works 
    return render_to_response("project_pages.html",{'data':data},context)

在我的模板 project_pages.html 我有这个:

<table>
    <tr>
        <td>field 1</td>
        <td>field 2</td>
        <td>field 3</td>
    </tr>

    {% for author, values in data.items %}
    <tr>
        <td>{{author}}</td>
        {% for v in values.0 %}
        <td>{{v}}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

并且不呈现对模板的响应。并且在终端中没有显示任何错误。该功能正在工作,因为它正在打印数据。

如果尝试返回如下内容:

return render_to_response("project_pages.html",{'data':data})

最后没有上下文(这是不同的调用)它给了我下一个错误:

"You must enable the 'sekizai.context_processors.sekizai' template "
TemplateSyntaxError: You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates.

在我的 settings.py 我有 sekizai:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'cms.context_processors.media',
    'sekizai.context_processors.sekizai',
    'sekizai.context.SekizaiContext',

)

那么,我该怎么办?

【问题讨论】:

  • 你设置了什么上下文?
  • context = RequestContext(request)
  • 我在 django 中尝试了一个没有安装 django-cms 的新测试项目,以查看此调用 (return render_to_response("project_pages.html",{'data':data}) 是否有效,它是工作正常,在我的模板中它向我展示了该表,但使用 django-cms 只是
    .

标签: python django django-cms render-to-response django-sekizai


【解决方案1】:

查看上下文变量有什么。它应该有这样的东西:

例子:

from django.template import RequestContext

def view(request):
   ...

   return render_to_response(
                "project_pages.html",
                {'data':data}, 
                context_instance=RequestContext(request),                   
           ) 

【讨论】:

  • 相同的结果,在我的模板中只是:
    field 1 field 2 field 3
    还有一件事,在这个模板中,我有一个带有提交操作的表单来解析 django 一些文本。我在javascript中创建了一个停止页面刷新的函数。有问题吗?
  • 可能是个问题。将通过刷新整个页面来显示发布响应。如果不想刷新页面,可以使用ajax查询提交表单。
  • 非常感谢。现在正在工作。但是如果页面没有用 ajax 刷新,我也会遇到同样的问题。直到现在我还没有使用过 ajax
  • Ajax 允许您在不刷新页面的情况下向/从服务器发送和接收数据 (json)。那么,您的问题出在哪里?
【解决方案2】:

来这里寻找 django 1.8 或更高版本的人应该使用以下代码。 现在,如果您正在编写自定义视图,则不会从 setting.py 文件中直接选择模板上下文处理器。

使用下面的代码使其工作:

from sekizai.context import SekizaiContext

def view(request):
...


    return render_to_response('template.html', 
                            SekizaiContext(request, {'data': data}))

【讨论】:

    【解决方案3】:

    我想通了。

    在此模板中,当用户按下按钮将一些信息提交给 django 进行处理时,我不想丢失一些信息,因此我在 javascript 中创建了一个允许提交但不刷新的函数这页纸。 不刷新内容保持静态,响应中的数据变量不会发送到模板

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2022-07-22
      • 2017-05-27
      • 1970-01-01
      • 2016-06-07
      • 2017-03-29
      • 2017-10-26
      • 2018-05-20
      • 2018-11-05
      相关资源
      最近更新 更多