我的任务是从数据库中检索数据并将其显示在HTML表格中。我使用数据库中的几列来显示页面,但它没有给出结果。请告诉我应该怎么做。它给了我一张空白的桌子。
<!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)),
)
答案 0 :(得分:2)
Testbeds
使用此行,您将向模板发送名为Testbeds
的变量 - 而不是名为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 %}
更改为Settings-Editor-Code Style-Java-Wrapping and Braces-Chained method calls
。
Chop down if long