{% extends 'app/layout.html' %}
{% block content %}
<div class="container">
<div class="users_orders">
<table class="table-bordered">
<tr>
<td coslpan="2" >Имя пользователя</td>
<td coslpan="2" >Почтовый индекс</td>
<td coslpan="2" >Город</td>
<td coslpan="2" >Улица</td>
<td coslpan="2" >Дата заказа</td>
<td coslpan="2" >Статус оплаты</td>
</tr>
{% for order in users_orders %}
<tr>
<td>{{ order.nickname }}</td>
<td>{{order.postal_code}}</td>
<td>{{order.city}}</td>
<td>{{order.street}}</td>
<td>{{order.created}}</td>
<td>{{order.paid}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endblock %}
class Order(models.Model):
username = models.ForeignKey(User,verbose_name ="Заказчик", default=False)
product_order = models.ForeignKey(Product, related_name='product', default=False)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
postal_code = models.CharField(max_length=20)
city = models.CharField(max_length=100)
street = models.CharField(max_length=100, default="s")
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
verbose_name = 'Заказ'
verbose_name_plural = 'Заказы'
def __str__(self):
return 'Order {}'.format(self.id)
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
from django.contrib import admin
from .models import Order, OrderItem
class OrderItemInline(admin.TabularInline):
model = OrderItem
raw_id_fields = ['product']
class OrderAdmin(admin.ModelAdmin):
list_display = ['id','username','product_order', 'first_name', 'last_name', 'email',
'postal_code', 'city', 'paid',
'created', 'updated',]
list_filter = ['paid', 'created', 'updated']
inlines = [OrderItemInline]
admin.site.register(Order, OrderAdmin)
def my_orders(request):
products = Product.objects.all()
order_items = Order.objects.filter(user_id=request.user)
assert isinstance(request, HttpRequest)
return render(
request,
'app/my_orders.html', {'products': products, 'order_items': order_items,}
)
def my_orders(request):
"""Renders the about page."""
products = Product.objects.all()
myorders = Order.objects.filter(user_id=request.user)
orders = request.user.orders.all()
order_items = Order.items.all()
product_ordered = OrderItem.objects.all()
assert isinstance(request, HttpRequest)
return render(
request,
'app/my_orders.html', {'products': products, 'product_ordered': product_ordered, 'myorders': myorders, 'orders':orders, 'order_items': order_items}
)
{% for item in order_items %}
<ul>
<li>{{ item.product}}</li>
<li>{{ item }}</li>
</ul>
{% endfor %}