简体   繁体   English

从sqllite获取数据并将其显示在Django中的html页面上

[英]Get data from sqllite and display it on html page in Django

I'am trying to display data from the database file which has the value Age : 50 but i alway get "Age object" displayed in the html. 我正在尝试显示数据库文件中的数据,该文件的值年龄:50,但是我总是在HTML中显示“年龄对象”。 I'm very new too Django. 我也是Django的新手。 Here is the code 这是代码

//base.HTML displays :
Age object

//base.html code :
<body>
{{ obj }}
</body>

//views.py : 
 def home(request):
    obj = Age.objects.all()
    return render_to_response("base.html",{'obj': obj})

//models.py
class Age(models.Model):
        age = models.CharField(max_length=100)

simply obj is an array of objects, you have to print the attribute of the object . 只是obj是一个对象数组,您必须打印该对象的属性

If you want to show only one age (the first) you have to do: 如果您只想显示一个age (第一个),则必须执行以下操作:

//views.py : 
 def home(request):
    obj = Age.objects.all()[0]
    return render_to_response("base.html",{'obj': obj})

//base.html code :
<body>
{{ obj.age }}
</body>

您需要指定要显示的字段。

{{ obj.age }}

您需要在模板中执行obj.age,或者在返回年龄的对象上实现str或unicode方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM