初学者Django/Python/Postgres-dev无法将模型数据输出到html文件
我一直在为Django学习一些基本的在线Youtube教程,这些教程很棒,但在过去的几天里,我一直在努力使用我的测试表应用程序,无法在我的index.html文件中的示例Postgresql表上显示表数据。当我创建Postgres模型(本地托管)时,它与管理页面中的数据一起正确显示,我的索引页面从url和视图文件配置中成功加载,但具体缺少表数据,我希望获得一些关于我的错误操作的指导。我知道URL和视图有更多的动态配置,但我试图先让一个基本配置正常工作初学者Django/Python/Postgres-dev无法将模型数据输出到html文件,django,postgresql,Django,Postgresql,我一直在为Django学习一些基本的在线Youtube教程,这些教程很棒,但在过去的几天里,我一直在努力使用我的测试表应用程序,无法在我的index.html文件中的示例Postgresql表上显示表数据。当我创建Postgres模型(本地托管)时,它与管理页面中的数据一起正确显示,我的索引页面从url和视图文件配置中成功加载,但具体缺少表数据,我希望获得一些关于我的错误操作的指导。我知道URL和视图有更多的动态配置,但我试图先让一个基本配置正常工作 models.py file: from
models.py file:
from django.db import models
class products(models.Model):
Phone = models.TextField(primary_key=True)
Version = models.TextField(max_length=100)
Price = models.FloatField()
Sales = models.IntegerField()
class Meta:
db_table = "productlist"
Url file:
from django.contrib import admin
from django.urls import path
from . import views
app_name = 'Test_table'
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.sales),
]
views.py
from django.shortcuts import render
from Test_table.models import products
# from django.http import HttpResponse
def sales(request):
ph_data = products.objects.all(),
return render(request, 'Test_table/index.html', {'ph_data': ph_data})
html文件:
<table border='3'>
<tr>
<th>Phone</th>
<th>Version</th>
<th>Price</th>
<th>Sales</th>
</tr>
{% for i in ph_data %}
<tr>
<td><h3>{{i.Phone}}</h3></td>
<td><h3>{{i.Version}}</h3></td>
<td><h3>{{i.Price}}</h3></td>
<td><h3>{{i.Sales}}</h3></td>
</tr>
{% endfor %}
</table>
电话
版本
价格
销售额
{ph_数据%中的i的百分比}
{{i.Phone}
{{i.Version}
{{i.Price}}
{{i.Sales}}
{%endfor%}
只需从ph\u data=products.objects.all(),
中删除,,其余就可以了
正确代码:
from django.shortcuts import render
from Test_table.models import products
# from django.http import HttpResponse
def sales(request):
ph_data = products.objects.all()
return render(request, 'Test_table/index.html', {'ph_data': ph_data})
并确保将admin中的数据添加到products表中以进行查看。视图中的逗号位于.all()之后。此外,您的编码样式已关闭