简体   繁体   中英

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

I'm trying to print my products from sqlite the problem is that the result in the browser is the code itself and not the value.

views.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})

index.html:

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

The result in the browser is:

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

In order to fix this you need to check index.html by running the server

python manage.py runserver

and going to the url that leads to index.html

if the result in the browser is like the one you said, are you opening the HTML file directly? because if Django rendered the template it wouldn't show this. even if the data is wrong or there is nothing, it'll just display nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM