我一直在尝试访问模板中的数据库,到目前为止我一直没有成功。
这是我一直在尝试访问的模型类:
class Questionnaire(models.Model):
name = models.CharField(max_length=128)
redirect_url = models.CharField(max_length=128, help_text="URL to redirect to when Questionnaire is complete. Macros: $SUBJECTID, $RUNID, $LANG", default="/static/complete.html")
def __unicode__(self):
return self.name
def questionsets(self):
if not hasattr(self, "__qscache"):
self.__qscache = \
QuestionSet.objects.filter(questionnaire=self).order_by('sortid')
return self.__qscache
class Meta:
permissions = (
("export", "Can export questionnaire answers"),
("management", "Management Tools")
)
以下是我views.py
的样子:
def question_list(request):
question_info = Questionnaire.objects.all()
question_data = {
"question_detail" : question_info
}
print question_data
return render_to_response('questionnaire/templates/quizzes.html', question_data, context_instance=RequestContext(request))
最后是我的模板quizzes.html
:
{% for question in question_detail %}
<h3>{{ question.name }}</h3>
{% endfor %}
答案 0 :(得分:0)
这种方式不简单吗?
views.py:
def question_list(request):
questions = Questionnaire.objects.all()
return render_to_response('questionnaire/templates/quizzes.html',{'questions':questions}, context_instance=RequestContext(request))
html的
{% for question in questions %}
<h3>{{ question.name }}</h3>
{% endfor %}