gpt4 book ai didi

html - 无法在 Django 1.10 中显示数据库中的数据?

转载 作者:行者123 更新时间:2023-11-29 12:35:58 27 4
gpt4 key购买 nike

我是 django 的新手,我无法显示来自 postgres 数据库的数据,我的数据库中有以下数据,

产品表中的数据

1 "测试""图片.jpg"1

3 "样本""图片.jpg"2

我的代码是,

views.py

from django.shortcuts import render
from products.forms import productsform
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from products.models import products
def productsview(request):
if request.method == 'POST':
form = productsform(request.POST)
if form.is_valid:
name = request.POST.get('name')
image = request.POST.get('image')
code = request.POST.get('code')
products_obj = products(name=name,image=image,code=code)
products_obj.save()
return HttpResponse("Register Successfull")
else:
form = productsform()
return render(request,'products/products_view.html',{'form':form,})

def display(request):
query_results = products.objects.all()
return render(request, 'products/myview.html')

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.productsview, name='products'),
url(r'^display', views.display, name='display'),
]

模型.py

from __future__ import unicode_literals

from django.db import models

class products(models.Model):
name = models.CharField(max_length=15,unique=True)
image = models.ImageField(upload_to = 'pictures')
code = models.IntegerField()

myview.html

<table>
<tr>
<th>Name</th>
<th>Code</th>
</tr>
{% for item in query_results %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.code }}</td>
</tr>
{% endfor %}
</table>

最佳答案

您没有将数据从 display 函数发送到 View 。

return render(request, 'products/myview.html', {'query_results': query_results})

关于html - 无法在 Django 1.10 中显示数据库中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com