Django Template not displaying model data

柔情痞子 提交于 2020-01-05 07:41:09

问题


I've looked through different tutorials and Stack Overflow questions and I'm not sure why I'm still running into this issue:

I am running into an issue with displaying my model data onto my template, I can see that the python code is executing, but no matter what I've tried I can't get my data to come through, my relevant code snippets are below:

models.py

class GoogleData(models.Model):
    placeID = models.CharField(max_length=999)
    name = models.CharField(max_length=200)
    phoneNumber =  models.CharField(max_length=800)
    busAddress = models.CharField(max_length=2000)
    openinghours = models.CharField(max_length=9999)

Views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response, render, get_object_or_404
from django.template import Context, loader
from hoursofDEV.models import GoogleData

def home(request):
    entries = GoogleData.objects.all()[:5]
    return render_to_response('index.html', {'entries': entries,})  

index.html

 {% if entries %}
<ul>
{% for GoogleData in entries %}
    <li><a href="/GoogleData/{{ GoogleData.name }}/">{{ GoogleData.name }}</a></li>
{% endfor %}
</ul>
{% else %}
    <p>Where's the Data?.</p>
{% endif %}

With the code that I've displayed, I constantly see my else "Where's the Data?.", I have hundreds of rows in GoogleData but I can't get any of them to show on the html page

Any guidance or pointing out of my newbie mistakes will be extremely helpful.

Thanks!


回答1:


you didn't add the RequestContext, your return statement should look like >>

return render_to_response('index.html', {'entries': entries}, RequestContext(request))

and don't forget to import the RequestContext >> from django.template import RequestContext



来源:https://stackoverflow.com/questions/15191393/django-template-not-displaying-model-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!