First pass Id as an argument along with request to that particular view function
On Thu, 24 Jun, 2021, 12:06 AM Parul., <parul.281997@gmail.com> wrote:
Hi ,I am doing : product_obj=Product.objects.get(id=product_id)in the cart_update function in views.pybut when trying to print this object... i am not getting anything... is this function is not working?--On Wed, Jun 23, 2021 at 11:38 PM Aadil Rashid <aadil10121998@gmail.com> wrote:Pass I'd to the view you are you are using,And then get products thrid that I'd by simple ORM quriyProduct = model name.objects.get(id=id)--On Wed, 23 Jun, 2021, 11:35 PM Parul., <parul.281997@gmail.com> wrote:Hi,--I am working on an ecommerce website. I am facing an error. Can anyone please help me solve this error.I am not able to fetch the PRODUCT_ID1. views.py (APP-CART)-------------------------------------------------------------------------------------------------------from django.shortcuts import render,redirect
from .models import Cart
from new_app.models import Product
# Create your views here.
def cart_home(request):
cart_obj,new_obj=Cart.objects.new_or_get(request)
products=Cart.objects.all()
return render(request,'carts/home.html',{})
def cart_update(request):
print(request.POST)
# print(dict(request.POST.items()))
# print("in func")
product_id=1
print('id below')
print(product_id) // not able to get the value of product id in console
product_obj=Product.objects.get(id=product_id)
cart_obj,new_obj=Cart.objects.new_or_get(request)
if product_obj in cart_obj.products.all():
cart_obj.products.remove(product_obj)
else:
cart_obj.products.add(product_obj)
return redirect('home')--------------------------------------------------------------------------------------------2. models.py (cart)from django.db import models
from django.conf import settings
from new_app.models import Product
from django.db.models.signals import pre_save,post_save,m2m_changed
User=settings.AUTH_USER_MODEL
class CartManager(models.Manager):
def new_or_get(self,request):
cart_id=request.session.get("cart_id",None)
# qs=self.get_queryset().filter(id=cart_id)
qs=self.get_queryset().only('products')
print(qs)
if qs.count()==1:
new_obj=False
cart_obj=qs.first()
print('cart obj below')
print(cart_obj)
if request.user.is_authenticated and cart_obj.user is None:
cart_obj.user=request.user
cart_obj.save()
else:
cart_obj=Cart.objects.new_cart(user=request.user)
new_obj=True
request.session['cart_id']=cart_obj.id
return cart_obj,new_obj
def new_cart(self,user=None):
user_obj=None
if user is not None:
if user.is_authenticated:
user_obj=user
return self.model.objects.create(user=user_obj)
class Cart(models.Model):
user=models.ForeignKey(User,null=True,blank=True,on_delete=models.CASCADE)
products=models.ManyToManyField(Product,blank=True)
subtotal=models.DecimalField(default=0.00,max_digits=100,decimal_places=2)
total=models.DecimalField(default=0.00,max_digits=100,decimal_places=2)
timestamp=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now=True)
objects=CartManager()
def __str__(self):
return str(self.id)
def m2m_changed_cart_receiver(sender,instance,action,*args,**kwargs):
print(action)
if action=='post_add' or action=='post_remove' or action=='clear':
products=instance.products.all()
total=0
for x in products:
total += x.price
if instance.subtotal != total:
instance.subtotal=total
instance.save()
m2m_changed.connect(m2m_changed_cart_receiver,sender=Cart.products.through)
def pre_save_cart_receiver(sender,instance,*args,**kwargs):
if instance.subtotal>0:
instance.total=instance.subtotal + 10
else:
instance.total=0.00
pre_save.connect(pre_save_cart_receiver,sender=Cart)OUTPUT IN CONSOLE:<QueryDict: {'csrfmiddlewaretoken': ['FMk2gTq6XXxZ2HU40I6h4b3WtPl59Drf1urwUNufDZUeSFPMzGNwU4L1QuGCiCbB'], 'product_id':
['']}> -------- GETTING EMPTY DICTIONARY INSTEAD OF GETTING PRODUCT ID i.e. 1
id below
1
<QuerySet [<Cart: 13>]>So, I am unable to fetch the productc id there besides the csrf....i tried to individually print the product id.. which came as 1...(written under"id below" in output)Can anyone pls help me with this.Also, adding update_cart.html<form method='POST' action='{% url 'update' %}'class="form ">{% csrf_token %}
<input type="hidden" name='product_id' vallue= "{{ product_id }}">
{% if product in cart.products.all %}
<button type="submit" class="btn btn-link">remove</button>
{% else %}
<button type="submit" class="btn btn-success">add to cart</button>
{% endif %}
</form>
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHjHRFpf_EfU%3DSSFFTMOeb5JkuNKL%3DuSNVtp782A2r_R-M0_DA%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAYXZx-x2ymvZ2sWiLK9RoEdxZJWa8-5po%2BtJqu04z9UB7cqkA%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHjHRFpn9RjG6ZJzVs3k4oVie-fp8%2BQ%3D8hqBh-bRxHUBMrCMZA%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAYXZx_cWaLT1uMPQ9pYvBCQnEWN1S%3D81UHpOEPBBDUy%2B3U56g%40mail.gmail.com.
No comments:
Post a Comment