0

In my views.py I add all transactions from the database to the request context. I want to display the data in a dashboard table. How do I do this using a for loop?

I can use the following to print out all data, but it doesn't fit into the table.

{% for transaction in transactions %}
{{transaction.date}} {{transaction.event}} {{transaction.total}}
{% endfor %}

The HTML code

<div class="sales-boxes">
        <div class="recent-sales box">
          <div class="title">Transaction history</div>
          <div class="sales-details">
            <ul class="details">
              <li class="topic">Date</li>
              <li><a href="#">Date 1</a></li>
              <li><a href="#">Date 2</a></li>
            </ul>
            <ul class="details">
            <li class="topic">Event</li>
            <li><a href="#">Event 1</a></li>
            <li><a href="#">Event 2</a></li>
          </ul>
          
          <ul class="details">
            <li class="topic">Total</li>
            <li><a href="#">$1</a></li>
            <li><a href="#">$2</a></li>
          </ul>
          </div>
          <div class="button">
            <a href="#">See All</a>
          </div>
        </div>

My views.py code used to render the page

from .models import Transaction

def earner_dashboard(request):
    transactions = Transaction.objects.all()
    return render(request, 'dashboard.html', context={"transactions":transactions})

1 Answer 1

1

You can use bootstrap to create columns.

<div class="sales-boxes">
    <div class="recent-sales box">
      <div class="title">Transaction history</div>
      <div class="row">
          <div class="col-4"> Date </div>
          <div class="col-4"> Event </div>
          <div class="col-4"> Total </div>
      </div>
      {% for transaction in transactions %}
      <div class="row">
          <div class="col-4"> {{transaction.date}} </div>
          <div class="col-4"> {{transaction.event}} </div>
          <div class="col-4"> {{transaction.total}} </div>
      </div>
      {% endfor %}

Don't forget to link bootstrap to your project by adding these lines to the head of your base template file.

   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
   <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'>
   <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css'>

You can use the table tag of HTML but is getting obsolete. Moreover, bootstrap is much more convenient to use.

Sign up to request clarification or add additional context in comments.

Comments

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.