python django запрос на сопоставление объектов: я получаю другой идентификатор объекта на избранной странице и не получаю тот же объект в индексе при клике - PullRequest
0 голосов
/ 24 апреля 2020

Шаблон Избранные:

    <div class="container">
        <div class="row">
            <div class="col-lg-6 mb-5 animate">
                <a href="{{ product_object1.image.url }}" class="image-popup"><img
                        src="{{ product_object1.image.url }}" class="img-fluid" alt="Template"></a>
            </div>
            <div class="col-lg-6 product-details pl-md-5 animate">
                <h3>{{ product_object1.name }}</h3>
                <div class="rating d-flex">
                    <p class="text-left mr-4">
                        <a href="#" class="mr-2">5.0</a>
                        <a href="#"><span class="ion-ios-star-outline"></span></a>
                        <a href="#"><span class="ion-ios-star-outline"></span></a>
                        <a href="#"><span class="ion-ios-star-outline"></span></a>
                        <a href="#"><span class="ion-ios-star-outline"></span></a>
                        <a href="#"><span class="ion-ios-star-outline"></span></a>
                    </p>
                    <p class="text-left mr-4">
                        <a href="#" class="mr-2" style="color: #000;">100 <span
                                style="color: #bbb;">Rating</span></a>
                    </p>
                    <p class="text-left">
                        <a href="#" class="mr-2" style="color: #000;">500 <span style="color: #bbb;">Sold</span></a>
                    </p>
                </div>
                <span class="status">{{ product_object1.discounted_price }}%</span>
                            <div class="overlay"></div>
                <p class="price">Price<span
                        class="mr-2 price-dc">(${{ product_object1.price }}/Lb)</span><span
                        class="price-sale">${{ product_object1.afterdiscount }}</span></p>
                <p>A small river named Duden flows by their place and supplies it with the necessary regelialia. It
                    is a paradisematic country, in which roasted parts of sentences fly into your mouth. Text should
                    turn around and return to its own, safe country. But nothing the copy said could convince her
                    and so it didn’t take long until.
                </p>
                <div class="row mt-4">
                    <div class="col-md-6">
                        <div class="form-group d-flex">
                            <div class="select-wrap">
                                <div class="icon"><span class="ion-ios-arrow-down"></span></div>
                                <select name="" id="" class="form-control">
                                    <option value="">Small</option>
                                    <option value="">Medium</option>
                                    <option value="">Large</option>
                                    <option value="">Extra Large</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="w-100"></div>
                    <div class="input-group col-md-6 d-flex mb-3">
                <span class="input-group-btn mr-2">
                    <button type="button" class="quantity-left-minus btn" data-type="minus" data-field="">
                   <i class="ion-ios-remove"></i>
                    </button>
                    </span>
                        <input type="text" id="quantity" name="quantity" class="form-control input-number" value="1"
                               min="1" max="100">
                        <span class="input-group-btn ml-2">
                    <button type="button" class="quantity-right-plus btn" data-type="plus" data-field="">
                     <i class="ion-ios-add"></i>
                 </button>
                </span>
                    </div>
                    <div class="w-100"></div>
                    <div class="col-md-12">
                        <p style="color: #000;">600 kg available</p>
                    </div>
                </div>
                <p><a href="cart.html" class="btn btn-black py-2 px-4">Add to Cart</a></p>
            </div>

Индекс шаблона:

 <div class="container">
        <div class="row">
            {% for products in product_object %}
                <div class="col-md-6 col-lg-3 ftco-animate">
                    <div class="product">
                        <a href="{% url 'featrued' products.id=id %}" class="img-prod"><img class="img-fluid" src="{{ products.image.url }}"
                                                          alt="{{products.image}}">
                            <span class="status">{{ products.discounted_price }}%</span>
                            <div class="overlay"></div>
                        </a>
                        <div class="text py-3 pb-4 px-3 text-center">
                            <h3><a href="#">{{ products.name }}</a></h3>
                            <div class="d-flex">
                                <div class="pricing">
                                    <p class="price"><span
                                            class="mr-2 price-dc">(${{ products.price }}/Lb)</span><span
                                            class="price-sale">${{ products.afterdiscount }}</span>
                                    </p>
                                    <div><span class="price-sale">{{ products.label }}</span></div>
                                </div>
                            </div>
                            <div class="bottom-area d-flex px-3">
                                <div class="m-auto d-flex">
                                    <a href="#"
                                       class="add-to-cart d-flex justify-content-center align-items-center text-center">
                                        <span><i class="ion-ios-menu"></i></span>
                                    </a>
                                    <a href="{% url 'featrued' products.id=id  %}"
                                       class="buy-now d-flex justify-content-center align-items-center mx-1">
                                        <span><i class="ion-ios-cart"></i></span>
                                    </a>
                                    <a href="#" class="heart d-flex justify-content-center align-items-center ">
                                        <span><i class="ion-ios-heart"></i></span>
                                    </a>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            {% endfor %}
        </div>

модель:

 Create your models here.
 CATEGORY_CHOICES = (('Fruits', 'Fruits'),
                ('Juices', 'Juices'),
                ('Veggies', 'Veggies'),
                ('Dried', 'Dried')
                )

LABEL_CHOICES = (('Farm Fresh', 'Farm Fresh'),
             ('Organic', 'Organic'),
             ('Extract', 'Extract')
             )

SIZE = (('LARGE', 'LARGE'),
    ('SMALL', 'SMALL'),
    ('MEDIUM', 'MEDIUM'),
    ('EXTRA LARGE', 'EXTRA LARGE')
    )

класс Featured_Product (models.Model):

name = models.CharField(blank=True, max_length=200)
price = models.FloatField(blank=True)
discounted_price = models.FloatField(blank=True)
category = models.CharField(max_length=7, blank=True, choices=CATEGORY_CHOICES)
label = models.CharField(max_length=10, blank=True, choices=LABEL_CHOICES)
image = models.ImageField(default='', upload_to='featured')
quantity = models.IntegerField(default=1)
afterdiscount = ComputedFloatField(blank=True, compute_from='calculation1')
totalprice = ComputedFloatField(blank=True, compute_from='calculation2')
taxes = models.FloatField(default=2)
stock = models.PositiveIntegerField(default=0)

@property
def calculation1(self):
    return self.price - (self.discounted_price / 100) * self.price

@property
def calculation2(self):
    return self.price * self.quantity + self.taxes

класс Продукт (models.Model):

name = models.CharField(max_length=200)
price = models.FloatField()
discounted_price = models.FloatField()
category = models.CharField(max_length=7, choices=CATEGORY_CHOICES)
label = models.CharField(max_length=10, choices=LABEL_CHOICES)
image = models.ImageField(upload_to='')
quantity = models.IntegerField(default=1)
afterdiscount = ComputedFloatField(default=0, compute_from='calculation1')
totalprice = ComputedFloatField(default=0, compute_from='calculation2')
taxes = models.FloatField()

@property
def calculation1(self):
    return self.price - (self.discounted_price / 100) * self.price

@property
def calculation2(self):
    return self.price * self.quantity + self.taxes

просмотр:

`домашняя страница def (запрос): product_object = Featured_Product.objects.all () pi c = Picture.objects.all () возвращает визуализацию (запрос, "Iamvegan / index. html", {'product_object': product_object, 'pi c': pic})

def featrued(request, id):
product_object1 = Featured_Product.objects.filter(id=id)
return render(request, "Iamvegan/Featured-product-single.html", {'product_object1':product_object1})

def product(request, id):
product_object2 = Product.objects.get(pk=id)
return render(request, "Iamvegan/product-product-single.html", {'product_object2':product_object2})`

` URL:

path('homepage/', views.homepage, name='homepage'),
path('<int:id>/', views.product, name='product'),
path('<int:id>/', views.featrued, name='featrued'),`
...