0
class ProductDetailSlugview(ObjectViewedMixin, DetailView):
    queryset = Product.objects.all()
    template_name = "products/product_detail.html"
    def get_context_data(self, *args, **kwargs):
        context=super(ProductDetailSlugview, self).get_context_data(*args , **kwargs)
        cart_object, new_object = Cart.objects.new_or_get(self.request)
        context['cart']=cart_object
        return context

this is my view

ValueError at /product/list/blackberry Cannot assign
" at 0x7f0488733860>>": "ObjectViewed.user" must be a "User" instance.
Request Method: GET Request
URL: http://127.0.0.1:8000/product/list/blackberry Django
Version: 2.1.3 Exception Type: ValueError Exception Value: Cannot
assign " object at 0x7f0488733860>>": "ObjectViewed.user" must be a "User"
instance. Exception
Location: /home/wiwigi/Desktop/django-virtual/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py
in set, line 210 Python
Executable: /home/wiwigi/Desktop/django-virtual/bin/python3 Python
Version: 3.6.5 Python Path: ['/home/wiwigi/Desktop/ecommerce_sample',
'/home/wiwigi/Desktop/ecommerce_sample',
'/home/wiwigi/Desktop/django-virtual/lib/python36.zip',
'/home/wiwigi/Desktop/django-virtual/lib/python3.6',
'/home/wiwigi/Desktop/django-virtual/lib/python3.6/lib-dynload',
'/usr/lib/python3.6',
'/home/wiwigi/Desktop/django-virtual/lib/python3.6/site-packages',
'/home/wiwigi/pycharm-2018.1.2/helpers/pycharm_matplotlib_backend']
Server time: mar, 26 Fév 2019 11:31:14 +0000

and my error code please help me

2 Answers 2

3

You are assigning an AnonymousUser object to the attribute user of an ObjectViewed instance. From the naming, my guess is that this happens in the ObjectViewedMixin.

For a more definite answer, you have to post the full stack trace and the relevant code.

2
  • Ya this happening because of AnonymousUser assignment to the attribute user of an ObjectViewed but in my case the attribute user may be a User object or it can be blank or Null then why i am getting same error. How to get rid from this error AnonymousUser or not able to view Detail page. Commented Nov 23, 2020 at 3:53
  • @RahulVerma I'd suggest you post a separate question with the full stack trace and the relevant code Commented Nov 23, 2020 at 8:21
-1

This is because of you are accessing this view with out login (So it shows AnonymousUser) so you need to add LoginRequiredMixin to your class as below to ensure that only logged in users can visit this view.

from django.contrib.auth.mixins import LoginRequiredMixin

class ProductDetailSlugview(LoginRequiredMixin, ObjectViewedMixin, DetailView):
    # rest of the code

Not the answer you're looking for? Browse other questions tagged or ask your own question.