渲染模板变量?

时间:2014-03-12 04:55:55

标签: django django-crispy-forms

模板呈现:

Hello, [<Student: Bob Frediricko>]. How are you?

但我希望它呈现:

Hello, Bob. How are you?

视图执行此操作:

q = Student.objects.filter(pk=1)

for f in survey_formset:
    f.helper.layout = Layout(HTML("""
                    Hello, {{ q }}. How are you?
                    """))

学生模特有......

def __unicode__ (self):
    return smart_unicode(self.first_name+" "+self.last_name)

感谢您的帮助:]

1 个答案:

答案 0 :(得分:2)

filter返回一个列表,这是您在渲染模板中看到的字符串化结果。

请改为尝试:

q = Student.objects.filter(pk=1)[0]

或者更好,因为你是通过pk选择的(这是唯一的):

q = Student.objects.get(pk=1)