1

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

1 Answer 1

1

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

actually {% for each question in the_question %} didn't work, but took out each {% for question in the_question %} worked. thanks!
Ah, it was assigning to each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.