Django App显示无结果

时间:2013-02-28 12:47:30

标签: django

我正在从一开始就从教程django开始一个名为Start the dark的应用程序。 我刚刚开始学习本教程,但我很难在html上显示结果。

应用程序假设显示事件的描述。事件的创建者及其发布时间以及键入链接时的事件。它没有任何显示。

我花了几个小时进行故障排除,到目前为止,我知道这个问题不是我的html表单,也不是我的URL,因为我做了一些测试。我认为它将models.py的结果导入我的views.py并执行它

我的models.py是:

from django.db import models
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.db.models.query import QuerySet


 class Event(models.Model):
    description = models.TextField()
    creation_date = models.DateTimeField(default = datetime.now)
    start_date = models.DateTimeField(null=True, blank= True)
    creator = models.ForeignKey(User , related_name = 'event_creator_set')
    attendees = models.ManyToManyField(User , through = "Attendance")
    latest = models.BooleanField(default=True)
    def __unicode__(self):
        return self.description
    def save(self, **kwargs):
        now = datetime.now()
        start = datetime.min.replace(year = now.year , month=now.month,day=now.day)
        end = (start + timedelta(days=1)) - timedelta.resolution
        Event.objects.filter(latest=True, creator=self.creator).filter(creation_date__range=(start,end)).update(latest=False)
    super(Event,self).save(**kwargs)

class Attendance(models.Model):
    user = models.ForeignKey(User)
    event = models.ForeignKey(Event)
    registration_date = models.DateTimeField(default=datetime.now)


    def __unicode__(self):
        return "%s is attending %s" %(self.user.username,self.event)

我的views.py

 from events.models import Event
from django.shortcuts import render_to_response
from django.template import RequestContext

def tonight(request):
    events = Event.objects.filter(latest=True)
    return render_to_response('events/tonight.html',{'events':events},context_instance = RequestContext(request))

我的tonight.html

 {% if events %}
 <ul>
{{events.description}}
{{events.start_date}}
{{events.creator}}
 </ul>
 {% endif %}

我的活动网址.CONF

 from django.conf.urls.defaults import *
 from django.contrib import admin
 from events import views
 urlpatterns = patterns('events.views',
         url(r'^tonight/$','tonight',name='ev_tonight')
 )

我的主要URLconf

  from django.conf.urls import patterns, include, url
  from django.contrib import admin
  from django.conf import settings
  from django.contrib.staticfiles.urls import staticfiles_urlpatterns
  from django.conf.urls.static import static
  # Uncomment the next two lines to enable the admin:
  # from django.contrib import admin
  admin.autodiscover()
  urlpatterns = patterns('',

      url(r'^events/',include('events.urls')),



  ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

  urlpatterns += staticfiles_urlpatterns()

其他一些模特。

 from django.db import models
 from django.contrib import admin
 class student(models.Model):
         First_name = models.CharField(max_length=30)
         Last_name = models.CharField(max_length=30)
     Age = models.BigIntegerField()
     Body = models.TextField()

     def __unicode__(self):
        return self.First_name

1 个答案:

答案 0 :(得分:1)

此处事件是一个查询集。请参阅django docs on querysets。您需要遍历其中的对象。像这样编辑模板代码;

{% if events %}
    {% for each_event in events %}
        <ul>
            {{each_event.description}}
            {{each_event.start_date}}
            {{each_event.creator}}
        </ul>
    {% endfor %}
{% endif %}