Answer the question
In order to leave comments, you need to log in
How to display the product name?
Hello.
There is a task, to deduce the list of the ordered goods. At the moment, information is displayed simply about the order. (id for example). Now you need to add the output of the name of the ordered product. You need to write something in views, but I don’t know what
models.py is
from django.db import models
import django
from shop.models import Product
from django.contrib.auth.models import User
class Order(models.Model):
nickname = models.ForeignKey(User, on_delete=models.CASCADE, default=False)
product = models.ForeignKey(Product, on_delete=models.CASCADE, default=False)
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())
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
order.nickname = request.user
"Вот здесь ,мне кажется, нужно что-то написать, для привязки id продукта к конкретному пользователю"
order.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# очистка корзины
cart.clear()
return render(request, 'orders/order/created.html',
{'order': order})
else:
form = OrderCreateForm
return render(request, 'orders/order/create.html',
{'cart': cart, 'form': form})
def my_orders(request):
orders = Order.objects.filter(nickname=request.user)
"Здесь тоже нужно добавить переменную, которая бы запрашивала конкретный id товара"
pdoruct = ...........
assert isinstance(request, HttpRequest)
return render(
request,
'app/my_orders.html', {'orders': orders,}
)
{% for order in orders %}
<ul>
<li>
{{order.id}}
</li>
<li>
{{order}}
</li>
</ul>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question