I am developing a restaurant management application in django. When I am trying to print the ordered items of a customer I am getting
testapp.Menu.None testapp.Menu.None
Only that many to many fiedls is showing none. Other are working fine.
These are my models:
categories=(
('Starters','Starters'),
('Maincourse','Maincourse'),
('Breakfast','Breakfast'),
('Desserts','Desserts'),
('Beverages','Beverages')
)
class Menu(models.Model):
name=models.CharField(max_length=200,null=True)
category=models.CharField(max_length=200,null=True,choices=categories)
price=models.IntegerField(null=True)
date_created=models.DateTimeField(auto_now_add=True,null=True)
def __str__(self):
return self.name
class Customer(models.Model):
name=models.CharField(max_length=200)
email=models.CharField(max_length=200)
phone=models.IntegerField()
address=models.TextField(max_length=200)
date_created=models.DateTimeField(auto_now_add=True,null=True)
def __str__(self):
return self.name
class Orders(models.Model):
status=(
('Out of delivery','Out of delivery'),
('Pending','Pending'),
('Order recieved','Order recieved'),
)
customer=models.ForeignKey(Customer,null=True,on_delete=models.SET_NULL)
items=models.ManyToManyField(Menu)
status=models.CharField(max_length=200,choices=status)
date_created=models.DateTimeField(auto_now_add=True,null=True)
This is my view.py file
def customer_info(request,pk):
customer=Customer.objects.get(id=pk)
order=customer.orders_set.all()
context={
'customer':customer,
'order':order
}
return render(request,'customer_info.html',context)
My html file
<table>
<tr>
{{customer.name}}
<br>
{% for order in order %}
{{ order.items }}
{% endfor %}
</tr>
</table>
Please help me to fix this.
CodePudding user response:
{% for order in order %}
will not work correctly.
Change your view:
def customer_info(request,pk):
customer = Customer.objects.get(id=pk)
orders = customer.orders_set.all()
context={
'customer': customer,
'orders': orders
}
return render(request,'customer_info.html',context)
And your template:
<table>
<tr>
<td>Name: {{customer.name}}</td>
<td>
Orders:
{% for order in orders %}
{{ order.items.all }}
{% endfor %}
</td>
</tr>
</table>