我的观点无法正确呈现。我对两个视图(index.html
和mq.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:
的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 %}
答案 0 :(得分:1)
您的for each
循环未向question
分配任何内容。
此:
{% for each in the_question %}
需要:
{% for question in the_question %}