本人初涉django,照著書敲的代碼,在視圖中定義了一個index函式,將從資料庫中獲取的資料傳給html檔案,但打開127.0.0.1:8000后什么都沒有顯示,加列印驗證是成功從資料庫中讀取資料了的。代碼如下,請各路大神幫忙看看,多謝!
# views.py
def index(request):
type_list = Product.objects.values('type').distinct()
# print(type_list)
name_list = Product.objects.values('name', 'type')
# print(name_list)
title = '首頁'
return render(request, 'index.html', context=locals(), status=200)
# models.py
class Product(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
type = models.CharField(max_length=20)
objects = models.Manager()
# urls.py
urlpatterns = [
path('', views.index)
]
# index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title }}</title>
<meta charset="UTF-8">
</head>
<body>
<div>
<ul id="cate_box" class="lf">
{% for type in type_list %}
<li>
<h3><a href="https://bbs.csdn.net/topics/#">{{ type.type }}</a></h3>
<p>
{% for name in name_list %}
{% if name.type == type.type %}
<span>{{ name.name }}</span>
{% endif %}
{% endfor %}
</p>
</li>
{% endfor %}
</ul>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/70114.html
標籤:其他
上一篇:我想在外網情況下訪問內網
