2014-06-06 151 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