Kaan San
Kaan San

Reputation: 103

Django - passing model object datas to template

I could'nt solve it. I want to pass specific datas from model to template. I had tried something could'nt figure it out.Can anyone help me out , should I need to write logic to view.py or is there easy way to pass data.

class Receipt(models.Model):

amount = models.DecimalField(max_digits=5, decimal_places=2)
vat = models.DecimalField(max_digits=5, decimal_places=2)
total_amount = models.DecimalField(max_digits=5, decimal_places=2)

def __str__(self):
    return str(self.total_amount)



#My view.py


class IndexView(TemplateView):
    template_name = "index.html"
    model = Receipt
class DetailView(TemplateView):
    template_name = "detail.html"
    model = ReceiptItem


#index.html

    <div class="panel panel-default">

  <div class="panel-heading">Market</div>
  <table class="table">
    <tr>
      {% for amount in objects %}
      <td>{{ amount }}</td>
      <td>{{ vat }}</td>
      <td>{{ total_amount }}</td>
      {% endfor %}
    </tr>
  </table>
</div>

Upvotes: 0

Views: 1571

Answers (1)

Jessie
Jessie

Reputation: 2475

TemplateView does not provide support for models. If you want a list of all of the objects so you can loop over them you will want a ListView, then switch your for loop to be:

{% for amount in object_list %}

https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-display/

Upvotes: 2

Related Questions