說我有一個整數值,我輸出到Django模板說x。我也有一張圖片,我想連續打印x次。我如何在模板中輸出這張圖片x次?在django模板中打印圖片x次
0
A
回答
1
快速而醜陋的黑客得到這個工作將使用make_list過濾器:
{% for i in "xxxx"|make_list %}
{{ image_html }}
{% endfor %}
這將打印圖像四次(因爲有字符串中的4個字符)。
一個清潔的方法是使用range()
傳遞一個列表到您的模板:
context['image_print_range'] = range(1, 5)
...
{% for i in image_print_range %}
{{ image_html }}
{% endfor %}
另一種解決方案(可能是最乾淨的一個)是一個自定義模板標籤或過濾器,例如:
@register.simple_tag
def print_multiple(value, count):
"""Print a value multiple times."""
return ' '.join([value] * count)
並在您的模板中:
{% print_multiple "foobar" 4 %}
1
要麼將其添加到視圖中的列表中x次,以便在{% for %}
標記中迭代它,要麼創建custom template tag以重複圖像url x次。
相關問題
- 1. 打印動態Django視圖模板
- 2. 打印出模板中的Django request.META
- 3. 打印在模板(未分類)dictonary DJANGO
- 4. Django的環流式模板內X次
- 5. Django在模板中x次後停止循環
- 6. 在反面打印模板
- 7. Java打印模板
- 8. Django:在模板中動態打印查詢集
- 9. 如何在Django模板中打印出精確的文本「{{text}}」?
- 10. Django在模板中打印sqlite數據庫數據
- 11. Django在模板中打印從視圖中傳遞的所有緩衝區
- 12. 遞增次數X次數(和打印)
- 13. 打印圖片框
- 14. 插入圖片到Django的模板
- 15. 在模板中Django「顯示一次」
- 16. 在flex中使用模板打印
- 17. 在Scala模板中打印HTML
- 18. 在模板中打印鍵/值對象
- 19. 如何在django的模板頁面上打印文件?
- 20. 在Django的覆蓋管理表單上打印模板變量
- 21. 如何打印換行符在Python Django的模板
- 22. 在模板中使用模板打印值class-C++
- 23. 重複圖片x次數
- 24. .net使用模板打印
- 25. 打印Resharper Live模板
- 26. 如何在Django模板中設置背景圖片?
- 27. Ruby:打印東西x次數
- 28. 打印字符串'X'次(無循環)
- 29. 調整圖片框中的圖片大小,以便在任何打印機上始終打印3.5「x2」圖片?
- 30. django模板中的圖像