我正在从教程 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)
我的观点.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))
我的今晚.html
{% if events %}
<ul>
{{events.description}}
{{events.start_date}}
{{events.creator}}
</ul>
{% endif %}
我的活动 URL.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