Shanzedul
Shanzedul

Reputation: 59

How to display Django array data

I am using Django and PostgreSQL. I have inserted some data into the models array field. My models are like

class PurchaseInvoice(models.Model):
    invoice = models.CharField(max_length=20)
    date = models.DateField(default=date.today)
    product_name = ArrayField(models.CharField(max_length=500))
    price = ArrayField(models.CharField(max_length=300))
    quantity = ArrayField(models.CharField(max_length=200))
    amount = ArrayField(models.CharField(max_length=300))

I have inserted data successfully, but when I try to query my data I get like ["['LNB Inverto', 'Dish']"]. I want to view my data in an HTML table. My views.py file is as the following:

def purchaseDetails(request,pk):
    invo_data = PurchaseInvoice.objects.filter(invoice=pk)
    return render(request,'purchase/invoice_details.html',{'invoice':invo_data})

And my HTML tables like:

{% for data in invoice %}
    <tr>
        <td>{{data.product_name}}</td>
        <td>{{data.price}}</td>
        <td>{{data.quantity}}</td>
    </tr>
{% endfor %}

Upvotes: 0

Views: 807

Answers (2)

Engr Tanveer sultan
Engr Tanveer sultan

Reputation: 173

Try to the following code. Your code is almost 100% right, I think, but I don’t know why the query returns like this: ["['LNB Inverto', 'Dish']"].

PurchaseInvoice.objects.all().filter(invoice=pk)

So try to do this query. Maybe your problem will be solved.

Upvotes: 0

iamdipta
iamdipta

Reputation: 366

{% for data in invoice %}
    <tr>
        <td>
            {% for name in data.product_name %}
                {{ name }}
            {% endfor %}
        </td>
        <td>{{ data.price }}</td>
        <td>{{ data.quantity }}</td>
    </tr>
{% endfor %}
happy coding :)

Upvotes: 1

Related Questions