简体   繁体   中英

Django accessing the database with specific model in template

I've been trying to access the database in my template and so far I have been unsuccessful.

Here is my model class I've been trying access:

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")
        )

Here is what my views.py looks like:

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))

and finally my template quizzes.html :

{% for question in question_detail %}
<h3>{{ question.name }}</h3>
{% endfor %}

Is not simpler this way?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM