2014-06-06 102 views
0

这是我index.html文件:Django的空白页

{% extends "base_site.html" %} 
{% block content %} 

{% if info %} 
    <ul> 
    {% for object in info %} 
     {{ Hello }} 
    {% endfor %} 
    </ul> 
{% else %} 
    <p>No objects are available.</p> 
{% endif %} 
{% endblock %} 

这是我views.py

from django.shortcuts import render 
from django.http import HttpResponse 
from django.template import RequestContext, loader 
from notendur.models import * 
from django.views import generic 

class IndexView(generic.ListView): 
    template_name = 'notendur/index.html' 
    context_object_name = "info" 

    def get_queryset(self): 
     """Return the last five published polls.""" 
     return Information.objects.all() 

这是我models.py

from django.db import models 
from django.contrib.auth.models import User 

# Create your models here. 

class Information(models.Model): 
    title = models.CharField(max_length = 200) 
    body = models.TextField() 
    date = models.DateTimeField() 

    def __unicode__(self): 
     return self.title 

class InformationChild(models.Model): 
    information = models.ForeignKey(Information) 
    child_text = models.CharField(max_length = 200) 

    def __unicode__(self): 
     return self.child_text 

当我启动服务器,但是,没有出现。这必须是一些url-link问题,因为else子句甚至没有激活。也许你还想要urls.py

让我知道你是否需要更多信息。

+0

用废话替换信息似乎会激活else子句。那么这肯定与信息不被发现有关。事实上这是不正确的。应该在那里而不是信息?我想获取信息对象。 – KSHMR

+0

将'get_queryset'中的'.all()'改为'.none()',看看是否{%else%}'被触发。 –

回答

3

错误不在使用info,它在for循环中:{{ Hello }}不是上下文中的项目。例如,使用{{ object.title }}

+0

感谢您的回复,这不会像我之前尝试的许多事情一样破坏我的代码。但它仍然产生else子句。我不认为我已经在任何地方定义了信息。 Django是否意识到我在谈论Information.objects.all()中的对象? – KSHMR

+1

现在我很困惑。你说你有一个空白页面,只有当你用废话取代'info'时才得到else子句。但是你现在说你无论如何得到else子句 - 这是什么? –

+0

我对我的帖子发表评论说现在会产生else子句。 – KSHMR