In short i am doing online store and in django admin i display authenticated users 'username' but there are users which didn't authenticated
So if authenticated users will order something it's okay i have their data. But if unauthenticated users will order? What to show in django admin? If i will request from user only his name from this no result. Because we know django need full authorization
models.py
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, through=CartProduct)
class Meta:
verbose_name = 'Cart'
verbose_name_plural = 'Cart'
def __str__(self):
return self.user.username # That for authenticated users
views.py
class CartView(View):
def get(self,request,*args,**kwargs):
cart_id = request.session.get('cart_id')
if cart_id == None:
cart = Cart()
cart.save()
request.session['cart_id'] = cart_id
cart = Cart.objects.get(id=cart_id)
product_id = request.GET.get('product')
delete_product = request.GET.get('delete')
if product_id:
product_instance = get_object_or_404(Product, id=product_id)
amount = request.GET.get('amount')
cart_product = CartProduct.objects.get_or_create(cart=cart, product=product_instance)[0]
if delete_product:
cart_product.delete()
else:
cart_product.amount = amount
cart_product.save()
return HttpResponseRedirect('/')
You could use IP adress, but that could be confusing / multiple people could share one.
So my suggestion would be to use Django's built-in session framework .
Pulling from this stackoverflow answer , you'll see that you can gather the current session key from the request
object.
Then it's just a matter of changing your Cart model :
from django.contrib.sessions.models import Session
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
session = models.ForeignKey(Session, null=True, blank=True)
products = models.ManyToManyField(Product, through=CartProduct)
class Meta:
verbose_name = 'Cart'
verbose_name_plural = 'Cart'
def __str__(self):
if self.user :
return self.user.username # For for authenticated users
else if self.session :
return self.session.session_key
else :
return "Anonymous"
And of course, you'll need to make sure that you add the request.session to your Cart object when you create it.
Finally, it seems the session object can still be empty sometimes, so you should look at force-saving it if you really don't want an "Anonymous" line in your admin. This answer makes force-saving the session quite simple :
if not request.session.session_key:
request.session.save()
session_id = request.session.session_key
Write a dispatch method inside the class so that you can check wether the user is authenticated request.user.is_authenticated
will return the status of user authentication. If the user is not authenticated then save the user as your need
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.