1

我的视图没有正确呈现。我对两个视图(index.htmlmq.html)都有相同的格式,并且index.html工作正常——它创建了一个项目符号列表,每个项目符号都有 1 个值。

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

楷模:

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:

索引.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 %}
4

1 回答 1

1

您的for each循环没有将任何内容分配给question.

这个:

{% for each in the_question %}

需要是:

{% for question in the_question %}
于 2012-10-14T06:26:27.647 回答