2012-10-16 26 views
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該特定課程的所有詳細信息,但在頁面上呈現時,它只顯示所需課程的標題。有誰知道我做錯了什麼?

+0

首先爲什麼不是你正在使用的選擇render_to_response – user1667633

+0

我通過Djangobook工作,這是如何顯示。 render_to_response在沒有的情況下有效的原因是否有特定的原因?還是僅僅爲了清晰/效率? – babbaggeii

+2

這應該工作。你確定你打電話給正確的觀點嗎?我不確定這個問題,但是''__unicode __()''''''''模型的方法應該返回一個unicode字符串而不是元組/列表。 – Rohan

回答

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)