Chris Yin
Chris Yin

Reputation: 791

Django View not rendering in HTML

My views are not rendering correctly. I have the same format for both views (index.html and mq.html), and index.html is working fine-- it creates a bulleted list with 1 value for each bullet.

However, mq.html creates the correct number of bullets, but the words don't show up on the page. Can anyone tell me what's wrong with the code? Models, Views, and HTML below.

Models:

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

Views:

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 %}

Upvotes: 1

Views: 645

Answers (1)

Nathan Villaescusa
Nathan Villaescusa

Reputation: 17629

Your for each loop is not assigning anything to a question.

This:

{% for each in the_question %}

Needs to be:

{% for question in the_question %}

Upvotes: 1

Related Questions