ArniReynir
ArniReynir

Reputation: 849

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. I'm very new too 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)

Upvotes: 0

Views: 1662

Answers (3)

zambotn
zambotn

Reputation: 785

simply obj is an array of objects, you have to print the attribute of the object.

If you want to show only one age(the first) you have to do:

//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>

Upvotes: 3

Chris Lacasse
Chris Lacasse

Reputation: 1562

You need to either do obj.age in the template, or implement str or unicode method on your object that returns the age.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599798

You need to specify what field to show.

{{ obj.age }}

Upvotes: 2

Related Questions