0

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)),
    )

1 Answer 1

2
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 %}
Sign up to request clarification or add additional context in comments.

4 Comments

That's the problem, but I'd suggest that changing the view so you can loop through through {% for testbed in testbed %} in the template would be better.
OperationalError at /base/ (1054, "Unknown column 'blog_testbeds.gateway_name' in 'field list'")
Do you have a column called gateway_name in your model?
@sufyanshahid It's not your view. Take a look here first: stackoverflow.com/questions/3787237/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.