Django View不以HTML格式呈现

时间:2012-10-14 06:20:49

标签: python django

我的观点无法正确呈现。我对两个视图(index.htmlmq.html)采用相同的格式,index.html工作正常 - 它会为每个项目符号创建一个带有1个值的项目符号列表。

但是,mq.html会创建正确数量的项目符号,但这些字词不会显示在页面上。谁能告诉我代码有什么问题?下面有ModelsViewsHTML

型号:

class Movie(models.Model):
    title = models.CharField(max_length = 500)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.title
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Question(models.Model):
    movie = models.ForeignKey(Movie)
    question_text = models.CharField(max_length = 1000)
    def __unicode__(self):
        return self.question_text

查看:

def index(request):
    r = Movie.objects.all().order_by('-pub_date')
    return render_to_response('mrt2/index.html', {'latest_movie_list': r})

def movie_questions(request, movie_id):
    p = Movie.objects.get(pk=movie_id)
    k = Question.objects.filter(movie=p)
    return render_to_response('mrt2/mq.html', {'movie':p, 'the_question':k})

HTML:

的index.html

<h1> Test </h1>

{% if latest_movie_list %}
    <ul>
    {% for movie in latest_movie_list %}
        <li><a href="/movie/{{ movie.id }}/">{{ movie.title }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No movies are available.</p>
{% endif %}

mq.html

{{ movie.title }}

{% if the_question %}
    <ul>
    {% for each in the_question %}
        <li><a href="/movie/{{ movie.id }}/{{ question.id }}/">{{ question.question_text }}</a>    </li>
    {% endfor %}
    </ul>
{% else %}
    <p>No questions have been asked.</p>
{% endif %}

1 个答案:

答案 0 :(得分:1)

您的for each循环未向question分配任何内容。

此:

{% for each in the_question %}

需要:

{% for question in the_question %}