Django只渲染单个变量

时间:2012-10-16 09:50:36

标签: django django-models django-templates

我请求一个调用以下视图的页面:

views.py:

from django.http import HttpResponse
from django.template import Context, loader
from mainsite.models import Courses

def individualCourse(request, course_short):

    c = Courses.objects.get(short=course_short)
    t = loader.get_template('course.html')
    cont = Context({
        'course_short': c.short,
        'course_title': c.title,
        'course_start': c.startDate,
        'course_end': c.endDate,
        'course_fees': c.fees,
        'course_description': c.description,
        'course_content': c.content
    })
    return HttpResponse(t.render(cont))

models.py:

from django.db import models

class Courses(models.Model):
    title = models.CharField(max_length=200)
    short = models.CharField(max_length=5)
    startDate = models.DateField(auto_now=False, auto_now_add=False)
    endDate = models.DateField(auto_now=False, auto_now_add=False)
    fees = models.IntegerField()
    description = models.TextField()
    content = models.TextField()

    def __unicode__(self):
        return  self.title, self.short, self.startDate, self.endDate, self.fees, self.description, self.content

course.html:

<html>
<head><title>Course</title></head>

<body>

<p>{{ course_title }}</p>
<p>{{ course_short }}</p>
<p>{{ course_start }}</p>
<p>{{ course_end }}</p>
<p>{{ course_fees }}</p>
<p>{{ course_description }}</p>
<p>{{ course_content }}</p>

</body>
</html>

我希望它显示该特定课程的所有详细信息,但是当它在页面上呈现时,它只显示所请求课程的标题。有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

试试这个 我不知道日期字段的返回类型说明符,它需要一些修正,但应该看起来像这样

def __unicode__(self):
    return u'%s (%s %s %s %d %s %s %s)' % (self.title, self.short, self.startDate, self.endDate, self.fees, self.description, self.content)