D
D
darkglare2020-11-19 17:46:50
Django
darkglare, 2020-11-19 17:46:50

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())


views.py
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})


Here information about the order, attention, about the order itself, about its availability is transferred to the template. Not the product name you want.
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,}
         )


my_orders.html
{% for order in orders %}
<ul>
    <li>
        {{order.id}}
    </li>
    <li>
        {{order}}
    </li>
</ul>
{% endfor %}


It is necessary that in addition to the order ID, the name of the ordered product is also transmitted.
Please tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-19
@syschel

{% for order in orders %}
<ul>
    <li>
        {{ order.id }}
    </li>
    <li>
        {{ order.product }}
    </li>
    <li>
        {{ order.product.name }}
    </li>
</ul>
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question