0
我请求调用以下视图中的页面:Django的仅呈现单个可变
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>
我想让它显示y该特定课程的所有详细信息,但在页面上呈现时,它只显示所需课程的标题。有谁知道我做错了什么?
首先为什么不是你正在使用的选择render_to_response – user1667633
我通过Djangobook工作,这是如何显示。 render_to_response在没有的情况下有效的原因是否有特定的原因?还是仅仅为了清晰/效率? – babbaggeii
这应该工作。你确定你打电话给正确的观点吗?我不确定这个问题,但是''__unicode __()''''''''模型的方法应该返回一个unicode字符串而不是元组/列表。 – Rohan