Django模板出现Invalid block tag错误,请求排查解决
解决Django模板中的TemplateSyntaxError问题
你的代码里存在两处直接引发TemplateSyntaxError的语法错误:
在遍历产品图片的循环中,你写成了{for image in product.images},但Django的模板循环标签必须用{% ... %}包裹,正确写法应为{% for image in product.images %}。
Django模板引擎只会识别{% %}包裹的块标签,单个大括号的{for}会被当作普通文本处理,后续的{% endfor %}就找不到对应的起始循环标签。引擎会误判这个endfor属于前面的if块,因此抛出“预期'elif'、'else'或'endif'”的错误提示。
修正后的核心代码
{% for product in products %} {% if product.isVariation == "yes" %} {% for var in product.variations %} <tr> <td><input type="checkbox"></td> {% for image in product.images %} <td><img src="{{image}}" style="height:250px; width:200px;"></td> {% endfor %} <td>{{var.sku}} Condition: {{var.condition}}</td> <td>{{product.name}}</td> <td>{{product.CreatedDateTime}}</td> <td><input type="text" class="form-control" id="formGroupExampleInput" placeholder="Amount" value={{var.units}}></td> <td><input type="text" class="form-control" id="formGroupExampleInput" placeholder="PKR" value={{var.price}}></td> <td> <div style="display: flex; justify-content: flex-end"> <button style="width: 100px;" type="button" class="btn btn-primary btn-outline-dark text-white bg-gradient"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16"> <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/> <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/> </svg> Edit</button> <button style="width: 100px;" type="button" class="btn btn-success btn-outline-dark text-white bg-gradient"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-down" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M3.5 10a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 0 0 1h2A1.5 1.5 0 0 0 14 9.5v-8A1.5 1.5 0 0 0 12.5 0h-9A1.5 1.5 0 0 0 2 1.5v8A1.5 1.5 0 0 0 3.5 11h2a.5.5 0 0 0 0-1h-2z"/> <path fill-rule="evenodd" d="M7.646 15.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 14.293V5.5a.5.5 0 0 0-1 0v8.793l-2.146-2.147a.5.5 0 0 0-.708.708l3 3z"/> </svg>Save</button> </div> </td> </tr> {% endfor %} {% endif %} {% if product.isVariation == "no" %} <tr> <td><input type="checkbox"></td> {% for image in product.images %} <td><img src="{{image}}" style="height:250px; width:200px;"></td> {% endfor %} <td>{{product.sku}} Condition: {{product.condition}}</td> <td>{{product.name}}</td> <td>{{product.CreatedDateTime}}</td> <td><input type="text" class="form-control" id="formGroupExampleInput" placeholder="Amount" value={{product.units}}></td> <td><input type="text" class="form-control" id="formGroupExampleInput" placeholder="PKR" value={{product.price}}></td> <td> <div style="display: flex; justify-content: flex-end"> <button style="width: 100px;" type="button" class="btn btn-primary btn-outline-dark text-white bg-gradient"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16"> <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/> <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/> </svg> Edit</button> <button style="width: 100px;" type="button" class="btn btn-success btn-outline-dark text-white bg-gradient"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-down" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M3.5 10a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 0 0 1h2A1.5 1.5 0 0 0 14 9.5v-8A1.5 1.5 0 0 0 12.5 0h-9A1.5 1.5 0 0 0 2 1.5v8A1.5 1.5 0 0 0 3.5 11h2a.5.5 0 0 0 0-1h-2z"/> <path fill-rule="evenodd" d="M7.646 15.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 14.293V5.5a.5.5 0 0 0-1 0v8.793l-2.146-2.147a.5.5 0 0 0-.708.708l3 3z"/> </svg>Save</button> </div> </td> </tr> {% endif %} {% endfor %}
内容的提问来源于stack exchange,提问作者pekka 123
相关产品推荐
相关产品推荐

