Rookies DJ
Rookies DJ

Reputation: 552

How do I get Django to display what, I want in database

Thanks for helping. I'm sorry I'm bad at programming.

Ok, I'm trying to take data out my database (SQLite 3) and display all at once.

This my code for views

def index(request):
tank = tank_system.objects.all()
args = {'tank':tank}
return render(request,'FrounterWeb/includes.html',args)

and here are my HTML that place

{% block content %}
<div class ="table-responsive-sm">
        <!-- tables tiles -->
  <table class ="table table-bordered table-responsive-sm">
  <tr>
        <th>Time</th>
        <th>EC</th>
        <th>pH</th>
        <th>Tank level</th>
        <th>room temptures</th>
        <th>Water temptrure</th>
  </tr>
      {% for tank_system in tank %}
      <tr>
          <td>time</td>
    <td>{{tank.EC}}</td>
    <td>{{tank.PH}}</td>
    <td>{{tank.WaterLevel}} lites</td>
          <td>{{tank.TempRoom}} C</td>
          <td>{{tank.TempWater}} C</td>
      </tr>

                {%endfor%}
  </table>
        </div>
{% endblock %} 

the end result objects.all() result

But if I would to modified to with objects.get(id=x)

Here modified views;

    def index(request):
        tank = tank_system.objects.get(id=1)
        args = {'tank':tank}
        return render(request,'FrounterWeb/includes.html',args)

and adjustment made in HTML;

{% block content %}
<div class ="table-responsive-sm">
        <!-- tables tiles -->
  <table class ="table table-bordered table-responsive-sm">
  <tr>
        <th>Time</th>
        <th>EC</th>
        <th>pH</th>
        <th>Tank level</th>
        <th>room temptures</th>
        <th>Water temptrure</th>
  </tr>

      <tr>
          <td>time</td>
    <td>{{tank.EC}}</td>
    <td>{{tank.PH}}</td>
    <td>{{tank.WaterLevel}} lites</td>
          <td>{{tank.TempRoom}} C</td>
          <td>{{tank.TempWater}} C</td>
      </tr>


  </table>
        </div>
{% endblock %}

and end result for this is objects.get(id=) result

I guarantee that the database pass jQuery data/object into my html, but now I'm stumped on how to fix this....

please help it should be simply fix

Upvotes: 1

Views: 46

Answers (1)

Will Keeling
Will Keeling

Reputation: 23054

The problem is the for loop in your template. You're using a loop variable called tank_system, but then referring to a non-existent variable called tank when you output each attribute.

Using the correct variable name of tank_system should resolve, e.g.

{% for tank_system in tank %}
<tr>
    <td>time</td>
    <td>{{tank_system.EC}}</td>
    <td>{{tank_system.PH}}</td>
    <td>{{tank_system.WaterLevel}} lites</td>
    <td>{{tank_system.TempRoom}} C</td>
    <td>{{tank_system.TempWater}} C</td>
</tr>
{%endfor%}

Upvotes: 1

Related Questions