使用視圖變量在view.py:不能在模板
class DisplayView(generic.ListView):
def get(self, request, **kwargs):
if request.method == "GET":
selection = request.GET.getlist("selection") # list of checkbox values with name 'selection
articles = Article.objects.all()
args = {'articles': articles, 'selection': selection}
print(selection)
print(articles)
return render(request, 'display.html', args)
在display.html:
{% extends 'myapp/base.html' %}
{%block contentblock %}
{% for section in selection %}
<h1>{{ section }}</h1>
<script language="javascript">console.log(selection)</script>
{% endfor %}
{% endblock %}
我可以在我的終端 '選擇' 和 '文章' 印上看到,這意味着視圖正確地從複選框中獲取數據。 但是,我的html中沒有任何內容出現,並且console.log引發錯誤,表示數據未傳遞給模板... 爲什麼?
是的,對不起,他們只是複製粘貼錯誤,我在這裏簡單的代碼。在我的原始代碼中,沒有這樣的錯誤。這不是問題。 – ben