在Django中显示来自db的数据 [英] display data from db in django

查看:55
本文介绍了在Django中显示来自db的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从db检索数据并在视图中显示,但是输出是空的.这是我的功能

I am trying retrive data from db and display in view but output is empty. Here is my function

from django.shortcuts import render
from django.http import HttpResponse
from pages.models import Contact
# from django.views import View
# Create your views here.
def home(request):
  return render(request, 'index.html', {'title':'Home Page'})
def contact(request):

  if(request.method == 'POST'):
    data = Contact(
      name = request.POST['name'],
      email = request.POST['email'], 
      address = request.POST['address'],
      city = request.POST['city'],
      zipcode = request.POST['zipcode'],  

    )
    data.save()

  dbdata = Contact.objects.all()
  return render(request, 'contact.html',  {'title':'Contact Page','row':dbdata})

这是我的模板,并在此表中显示数据

Here is my template and display data in this table

  <tbody>

            {% for row in rows %}
            <tr>
                <th>{{row.name}}</th> 
                <th>{{row.emai}}</th>
                <th>{{row.address}}</th>
                <th>{{row.city}}</th>
                <th>{{row.zipcode}}</th>
            </tr>
        {%endfor%} 
        </tbody>

我是django的初学者,也请告诉我如何在中调试代码django.

I am beginner in django.and also please tell me how to debug code in django.

推荐答案

传递上下文时出错..您在contex中使用键 row .但是您在调用 rows for循环中,将键更改为.并保持for循环不变.

there is a mistake in passing context.. you are using key row in contex.But you are calling rows in for loop.change key row to rows . and keep for loop as it is.

此处传递上下文字典键的变化

here change in passing context dictinary key

return render(request, 'contact.html',  {'title':'Contact Page','rows':dbdata})

如果这有效,请告诉我....

if this is working then let me know....

这篇关于在Django中显示来自db的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆