【问题标题】:Python code inside HTML file not working (Django)HTML文件中的Python代码不起作用(Django)
【发布时间】: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
  • 显示你的代码。
  • 对不起,我刚刚添加了两个文件的截图
  • 好吧,不要。在问题中以文本形式发布代码。

标签: python html django


【解决方案1】:

您传递给模板的上下文数据与您在模板中调用的变量不匹配。

来自您的 views.py 文件:

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))

然后在你的 cart.html 文件中:

    {% for product in products_list %}

您需要将 cart.html for 循环更改为 {% for product in cart_list %},因为 cart_list 是您添加到上下文中的变量。

【讨论】:

    【解决方案2】:

    使用 django,代码只有在 html 文件由 django 处理时才能在 html 中工作 - 这意味着您必须使用 django 的 html 模板处理工具之一。

    比较常用的有:render_to_stringget_template获取模板对象后跟.render()方法

    查看django template documentation here了解更多详情。

    【讨论】:

    • 谢谢。我刚刚添加了我的视图的屏幕截图,我在其中指定获取 product/cart.html 的模板。但是还是不行。。
    • 请不要添加屏幕截图。添加实际文本。 @NicoBar 似乎您的模板不起作用(cart.html)正在尝试使用名为products_list 的变量,但是代码仅传递了一个名为cart_list 的变量,所以我想这是您的问题。如果没有,您应该(以文本形式)提供您收到的完整错误信息、所有信息和引用,以及完整的文本代码。
    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多