Hello, I want to create a table with n-rows({{order.ticket_count}}), but I’m not able iterate in foor loop, get following error:
|Django Version:|4.2.9|
|Exception Type:|ValueError|
|Exception Value:|invalid literal for int() with base 10: '{{order.ticket_count}}'|
{% for order in orders %}
{% for item in "x"|ljust:"{{order.ticket_count}}" %}
<tr>
<td>{{item}}</td>
<td>-</td>
<td>order.price</td>
<td><a>Remove</a></td>
</tr>
{% endfor %}
{% endfor%}
Side note: When posting code, templates, or error messages, enclose that text between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (or template, etc), then another line of ```. (I’ve taken the liberty of modifying your original post.)
When you’re referencing a context variable inside a tag, you do not enclose it within the braces {{ ... }}
.
You don’t show the data or models that you are working with here, but it’s possible that you’re going to need to alter your approach. I think I may be able to guess what you’re trying to do, but I’m not sure.
Hello Ken, thanks for side note, I hope now it is ok. I removed the braces {{…}} but got a ValueError:
invalid literal for int() with base 10: 'order.ticket_count'
I want to do which it in python looks like, but I want to do it in Django:
for row in range(order.ticket_count):
print(row + '-' + order.event_id.ticket_price + 'Remove')
where order.ticket_count is integer and order.event_id.ticket_price is a float/integer.
from model.py
class SigningUp(models.Model):
PAYMENT_STATUS = [
("P", "Paid"),
("N", "Not paid"),
]
event_id = models.ForeignKey(Event, on_delete=models.DO_NOTHING, blank=False, null=False)
user_id = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True)
event_date = models.ForeignKey(EventDate, on_delete=models.SET_NULL, null=True)
signing_up_date = models.DateTimeField(auto_now_add=True)
ticket_count = models.IntegerField(blank=False, null=False)
status = models.CharField(max_length=1, choices=PAYMENT_STATUS, null=True)
from views.py:
def shopping_cart(request):
user = request.user
orders = SigningUp.objects.filter(user_id=user, status='N')
content = {'orders': orders}
return render(request, 'events/shopping_cart.html', content)
shopping_cart.html
{% for order in orders %}
{% for item in "x"|ljust:"order.ticket_count" %}
<tr>
<td>{{item}}</td>
<td>-</td>
<td>{{order.event_id.ticket_price}}</td>
<td><a>Remove</a></td>
</tr>
{% endfor %}
{% endfor%}
The issue here with your revised version is that you’ve got quotes around “order.ticket_count”, which means you’ve identified that as a string constant.
Also, I’m still not sure what you’re thinking you’re going to generate by that tag:
See the docs for ljust.
What I think you may really be looking for is the counter
attribute available in the for
loop.
Hello, I want to create table which looks like - 4 tickets, there is 4 lines + header in the table:
So if there is they order N tickets than there are N rows for each ticket, plus one header.
The order.ticket_count ticket variable contains the number of ticekts, and via for loop I want to “print” the table. The ‘ljust’ tag seems not proper for this case.
I hope now it is clear what I want. Thanks
I understand now, thanks. See my reference to the counter attribute at the end of my previous reply.
I still don’t know how should the input to for loop look like, now I get TypeError: 'int' object is not iterable
{% for item in order.ticket_count %}
<tr>
<td>{{forloop.counter}}</td>
<td>-</td>
<td>{{order.event_id.ticket_price}} €</td>
<td><a href="">Odobrat</a></td>
</tr>
{% endfor %}
You don’t need to iterate over order.ticket_count since you’re iterating over order in orders
. That loop goes away completely.
So nested loops are not working in Django templates?
orders = SigningUp.objects.filter(user_id=user,status=‘N’)
print(orders) → <QuerySet [<SigningUp: SigningUp object (4)>]>
I cannot use get method, because there can be more orders.
order which is from orders are “equal” to SigningUp model, so if orders.ticket_count=4 I want to do for loop which creates a table with 4 rows.
class SigningUp(models.Model):
PAYMENT_STATUS = [
("P", "Paid"),
("N", "Not paid"),
]
event_id = models.ForeignKey(Event, on_delete=models.DO_NOTHING, blank=False, null=False)
user_id = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True)
event_date = models.ForeignKey(EventDate, on_delete=models.SET_NULL, null=True)
signing_up_date = models.DateTimeField(auto_now_add=True)
ticket_count = models.IntegerField(blank=False, null=False)
status = models.CharField(max_length=1, choices=PAYMENT_STATUS, null=True)
Nested loops work fine. It just doesn’t make any sense to use them in this case, for the sample of the template you’ve provided.
You’re already iterating over orders
, which means each instance of order
inside that loop is going to be a different order.
I have two orders(2 instances) one has 4 tickets the second one has 3 tickets. First I have to iterate over orders, and then iterate over tickets. I want to display this:
Ok, so you’re looking to repeat the same data “ticket_count” number of times. And that also explains your original attempt to use the ljust
filter to create the arbitrary length string.
Now I think I understand what you’re trying to do here - and yes, I think your original attempt is appropriate here: {% for item in "x"|ljust:order.ticket_count %}
1 Like