0

I am using Django and I am trying to making a template tag to display the data from my database (sqlite3). So far this is my models.py

class Answer(models.Model):
    subject = models.ForeignKey(Subject, help_text = u'The user who supplied this answer')
    question = models.ForeignKey(Question, help_text = u"The question that this is an answer to")
    runid = models.CharField(u'RunID', help_text = u"The RunID (ie. year)", max_length=32)
    answer = models.TextField()

    def __unicode__(self):
        return "Answer(%s: %s, %s)" % (self.question.number, self.subject.surname, self.subject.givenname)

and here is my view.py

def answer_list(request):
    answer_info = Answer.objects.all()

    answer_data = {
    "answer_detail" : answer_info
    }

print answer_data
 return render_to_response('quizzes.html'', answer_data, context_instance=RequestContext(request))

to display the information that is in the database how should my quizzes.html template look like? so far I have this

{% for answer_list in answer_detail %}
<h3>{{ answer_list.question }}</h3>
<p>{{ answer_list.answer }}
{{ answer_list.runid }}</p>
{% endfor %}

I believe there is something slightly wrong with my for loop but unsure of what?

1
  • Have you gone through the tutorial? The first argument to render_to_response is the template name, not the URL. Commented Nov 27, 2013 at 6:54

1 Answer 1

3

answers_detail in your tag is not the same as the "answer_detail" key that you pass in your context dict. There's an extra s in the tag version.

Sign up to request clarification or add additional context in comments.

5 Comments

That doesnt seem to be the problem. What should the blank be in {% for ______ in answer_detail %}
It's an arbitrary name. You can put down whatever you like, then as long as you reference it with the same name, that won't be a problem.
That is what I thought. I dont understand why the db data is display in my template.
No I think maybe its in my URLS? my quizzes template is url(r'quizzes/$', (TemplateView.as_view(template_name="quizzes.html"))),
I think looking at one of the popular tutorials might be a good use of time. Try replacing that classed based view reference in your urlconf with url(r'quizzes/$', path.to.answer_list).

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.