【问题标题】:Django rendering Template tags on HTML Webpage in textDjango 在 HTML 网页上以文本形式呈现模板标签
【发布时间】:2015-02-13 19:05:52
【问题描述】:

我的 django HTML 标签在通过“for”循环运行时不起作用。如果我打印出变量,它会打印出普通列表。我什至尝试了不同的分配变量的方法。发生了什么事?

view.py

class CheckbackView(View):
    def post(self, request):
        statusly = request.POST.get("ConfigSubmit", "")
        list_init_config = statusly.splitlines()
        with open('/data/test.txt', 'w') as fout:
            fout.write(statusly)
            fout.close()
        converted = Con1812_to_891(statusly)
        converted_config = converted.config
        removed_list = converted.remove_list

        return render(request, 'thunderdome/compare_edit.html', context_instance = RequestContext(request, {'config': statusly, 'converted': converted.config, 'removed': removed_list, 'objection': converted}))

呈现如下:

HTML:

{ % for line in removed %}
    <li>{{ line|safe }}</li>
{ % endfor %}



{ % for line in objection.remove_list %}
    <li>{{ line }}</li>
{ % endfor %}

{{objection.remove_list}}

{{removed}}

【问题讨论】:

    标签: python django templates for-loop


    【解决方案1】:

    您的模板中的语法有点偏离;删除{% 之间的空格:

    {% for line in removed %}
        <li>{{ line|safe }}</li>
    {% endfor %}
    
    
    
    {% for line in objection.remove_list %}
        <li>{{ line }}</li>
    {% endfor %}
    

    【讨论】:

    • 我早该知道 Django 会对模板标签如此挑剔。
    • 在那个范围内,python或任何编程语言也很挑剔,因为你不能用循环代替for。
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 2017-05-04
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2021-10-28
    相关资源
    最近更新 更多