Reputation: 33
My task is to to retrieve data from database and show it in an HTML table. I am using a few columns from the database to show in the page, but its is not giving a result. Please tell me what I should do. It's giving me a blank table.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}/static/css/style.css"/>
<meta charset="utf-8">
<title> MY Site
</title>
</head>
<body>
<div class="container">
<div class="header">
<img src="/static/images/django-logo.png" width="300px;" height="75px" >
</div>
</div>
<div class="content">
<table border="1" style= "background-color:white; width:60%" cellspacing="0" cellpadding="" >
<tr>
<th>Name</th>
<th>tb_Name </th>
<th>Product_name </th>
<th>Domain_name </th>
</tr>
{% for key in Testbeds %}
<tr>
<td>{{ key.name }}</td>
<td>{{ key.tb_name }}</td>
<td>{{ key.product_name }}</td>
<td>{{ key.domain_name }}</td>
</tr>
{% endfor %}
</table>
</div>
<div class="footer">
<p> copy right django.com </p>
</div>
</div>
</body>
</html>
view.py
from django.shortcuts import render, HttpResponse,get_object_or_404
from datetime import datetime
from blog.models import Testbeds
def base(request):
obj = Testbeds.objects.all()
return render (request, 'base.html', {'obj': obj})
url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^base/$', 'blog.views.base', name="base"),
#url(r'^(?P<slug>[\w\-]+)/$', 'blog.views.post'),
url(r'^admin/', include(admin.site.urls)),
)
Upvotes: 0
Views: 68
Reputation: 3760
return render (request, 'base.html', {'obj': obj})
With this line, you are sending a variable called obj
to your template - not one called Testbeds
. Change Testbeds
in your template to obj
.
{% for o in obj %}
<tr>
<td>{{ o.name }}</td>
<td>{{ o.tb_name }}</td>
<td>{{ o.product_name }}</td>
<td>{{ o.domain_name }}</td>
</tr>
{% endfor %}
Upvotes: 2