python - 为什么我收到错误 : "Error: Render_to_response not defined"; Django

标签 python django

我正在学习 Django,我在网上找到了一个关于如何使用模板显示表格的非常基本的示例。我完全按照代码进行操作,但由于某种原因我得到了错误:

Error: Render_to_response not defined

这是我的views.py:

from django.shortcuts import render

def display(request):
    return render_to_response('template.tmpl', {'obj':models.Book.objects.all()})

这是我的 urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    # /table/
    url(r'^$', views.display, name='display'),
]

这是我的模板.tmpl:

<table>
<tr>
  <th>author</th>
  <th>title</th>
  <th>publication year</th>
</tr>
{% for b in obj %}
<tr>
  <td>{{ b.author }}</td>
  <td>{{ b.title }}</td>
  <td>{{ b.publication_year }}</td>
</tr>
{% endfor %}
</table>

这是我的模型.py:

from django.db import models

class Book(models.Model):
    author = models.CharField(max_length = 20)
    title = models.CharField(max_length = 40)
    publication_year = models.IntegerField()

我在网上寻找有关此错误的帮助,但所有问题似乎都比我面临的问题复杂得多。有什么我想念的吗?

最佳答案

您正在导入 render 但使用 render_to_response

替换render_to_response

from django.shortcuts import render

def display(request):
    return render(request, 'template.tmpl', {'obj':models.Book.objects.all()})

关于python - 为什么我收到错误 : "Error: Render_to_response not defined"; Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39324619/

相关文章:

python - 美汤找不到标签

sql - Django中的双外键?

javascript - Django 和 AngularJS

python - Django 做一个简单的 group by/count 语句

python - 安装海报(流式 HTTP 上传和多部分/表单数据编码)

python - 删除错误 : 'setuptools' is a dependency of conda and cannot be removed from conda's operating environment

python - Python 列表创建的性能

django - HTTP/1.0 301永久移动-Django

django - 向 Django 管理员添加用户友好的 json 编辑器

python - 与重复索引合并 - 行数大于预期