【发布时间】:2018-06-21 16:44:12
【问题描述】:
我有两个模板,一个叫做 index.html,另一个叫做 cart.html。 index.html 文件接受 python 代码,但如果我在 cart.html 中放入完全相同的代码,则无法识别 Python。 我正在与 Django 合作。我该如何解决这个问题?
python 代码工作的 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>List of car parts available:</th>
</tr>
{% for product in products_list %}
<tr>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
<td>
{% if product.in_cart == False %}
<a href=""></a>
</td>
<td>{{ product.ordered }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
python 代码不起作用的cart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>List of car parts available:</th>
</tr>
{% for product in products_list %}
<tr>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
<td>
{% if product.in_cart == False %}
<a href=""></a>
</td>
<td>{{ product.ordered }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
views.py
from django.http import HttpResponse
from django.template import loader
from .models import Product
# from django.shortcuts import render
def index(request):
products_list = Product.objects.all()
template = loader.get_template('products/index.html')
context = {'products_list': products_list}
return HttpResponse(template.render(context, request))
def cart(request):
cart_list = Product.objects.filter(in_cart == True)
template = loader.get_template('products/cart.html')
context = {'cart_list': cart_list}
return HttpResponse(template.render(context, request))
【问题讨论】:
-
除非您提供有关问题的足够详细信息,否则我怀疑任何人都能够提供帮助。我们看不到您的代码或设置或任何东西。请提供MCVE
-
显示你的代码。
-
对不起,我刚刚添加了两个文件的截图
-
好吧,不要。在问题中以文本形式发布代码。