[英]How to pass a variable to template in Django
我是django的新手,经过数小时的尝试,没有任何效果。
我有一个views.py:
class sspView(generic.ListView):
template_name = 'ssp/sspTableView.html'
context_object_name = 'ssp'
message = "yo, this is the message"
def message(request):
return render(request, 'ssp/sspTableView.html', {'message': message})
def get_queryset(self):
return googleData.objects.order_by('date')
我有一个template.html:
{% if ssp %}
<p>total click is: {{ message }}</p>
<table>
{% for googleData in ssp %}
<tr>
<td>{{ googleData.date }}</td>
<td>{{ googleData.account }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
Table呈现完美,但是该消息不会显示。
谢谢。
仅写以下行或将行移到if
循环之外。
<p>total click is: {{ message }}</p>
为什么不可见?
这是因为if
条件写在模板。 {% if ssp %}
您可以使用get_context_data
方法添加额外的上下文 :
def get_context_data(self, **kwargs):
context = super(sspView, self).get_context_data(**kwargs)
context['message'] = 'Hello, context!'
return context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.