Andrew Quoc-Anh Ho
Andrew Quoc-Anh Ho

Reputation: 131

Why am I getting the error: "Error: Render_to_response not defined"; Django

I'm learning Django, and I found a very basic example online on how to display tables using templates. I followed the code exactly, but for some reason I get the error:

Error: Render_to_response not defined

Here is my views.py:

from django.shortcuts import render

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

Here is my urls.py:

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

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

Here is my template.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>

Here is my models.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()

I've looked online for some help with this error, but all the problems seem to be much more complicated than the one I'm facing. Is there something I'm missing?

Upvotes: 3

Views: 2139

Answers (1)

Lord Salforis
Lord Salforis

Reputation: 602

You are importing render but using render_to_response

Replace render_to_response

from django.shortcuts import render

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

Upvotes: 4

Related Questions