-1

I got an error, ValueError at /app/top/ The view app.views.top didn't return an HttpResponse object. It returned None instead. I wrote codes in views.py

from .models import POST
from django.shortcuts import render

def top(request):
    contents = POST.objects.order_by('-created_at')
    render(request, 'top.html', {'contents': contents})

in models.py

from django.db import models

class POST(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()

in html

<body>
    <div>
         {% for content in contents.all %}
            <div>
                 <h2>{{ content.title }}</h2>
                 <p>{{ content.text }}</p>
            </div>
         {% endfor %}
    </div>
</body>

In model, several data of title&text is registed,so surely data is not empty.I think this is directory mistake, now my application structure is

-mysite(parent app)
 -app(child app)
  -templates
   -top.html
  -models.py
  -views.py

so I do not know what is wrong.How should I fix this?What is wrong in my codes?

user7676799
  • 105
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of https://stackoverflow.com/questions/15217193/django-didnt-return-an-httpresponse-object – dmitryro Jan 01 '18 at 02:42
  • @dmitryro I did not think so – user7676799 Jan 01 '18 at 03:20
  • In your code, you're optimistic about `for content in contents.all` - if this is None (and Python does not speculate - it just rejects unchecked troublesome collections) - this will generate an error, so prior to calling `.all` you need to make it 100 percent certain, that this object is real. In this case `I'm sure it's not empty` is not enough. – dmitryro Jan 01 '18 at 03:24

1 Answers1

0

You have to fix the content of the error message.

Every Django view have to return HTTP Response.

If you look at your view function, you do not return anything.

You should fix your view function like this.

def top(request):
    contents = POST.objects.order_by('-created_at')
    return render(request, 'top.html', {'contents': contents})
Youngil Cho
  • 159
  • 5