我是django的新手,我无法显示postgres数据库中的数据,我的数据库中有以下数据,
产品数据表
1“test”“images.jpg”1
3“sample”“images.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'), ]models.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>I am new to django and i cannot able to display data from postgres database, i have following data in my database,
Data in products Table
1 "test" "images.jpg" 1
3 "sample" "images.jpg" 2
and my code is ,
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'), ]models.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功能向视图发送数据。
return render(request, 'products/myview.html', {'query_results': query_results})You're not sending the data to the view from the display function.
return render(request, 'products/myview.html', {'query_results': query_results})无法在Django 1.10中显示数据库中的数据?(Cannot able to display data from database in Django 1.10?)我是django的新手,我无法显示postgres数据库中的数据,我的数据库中有以下数据,
产品数据表
1“test”“images.jpg”1
3“sample”“images.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'), ]models.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>I am new to django and i cannot able to display data from postgres database, i have following data in my database,
Data in products Table
1 "test" "images.jpg" 1
3 "sample" "images.jpg" 2
and my code is ,
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'), ]models.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功能向视图发送数据。
return render(request, 'products/myview.html', {'query_results': query_results})You're not sending the data to the view from the display function.
return render(request, 'products/myview.html', {'query_results': query_results})
发布评论