簡體   English   中英

從 views.py 到 html 的 python django 變量顯示值的代碼

[英]python django variables from views.py to html show the code intead of the value

我正在嘗試從 sqlite 打印我的產品問題是瀏覽器中的結果是代碼本身而不是值。

視圖.py:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Product


def index(request):
    products = Product.objects.all()
    return render(request, 'index.html', {'dict': products})

索引.html:

<ul>
    {% for prod in dict %}
        <li>{{ prod.name }}</li>
    {% endfor %}
</ul>

瀏覽器中的結果是:

{% for prod in dict %}
{{ prod.name }}
{% endfor %}

為了解決這個問題,您需要通過運行服務器檢查 index.html

python manage.py runserver

並轉到導致 index.html 的 url

如果瀏覽器中的結果像你說的那樣,你是直接打開 HTML 文件嗎? 因為如果 Django 呈現模板它不會顯示這個。 即使數據錯誤或什么都沒有,它也不會顯示任何內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM