我想更改现有站点,以便非注册用户可以查看它。每个人都可以访问产品列表(wa母页面)。但是当我要访问单个产品时出现错误: “ AnonymousUser”对象不可迭代
我认为问题出在我的product / models.py中 我将显示完整的代码,以便有人帮助。
from itertools import chain
from random import shuffle
from django.contrib import messages
from django.db import models
from django.conf import settings
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey
from taggit.models import TaggedItemBase
from wagtail.api import APIField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailimages.models import Image
from wagtail.wagtailsearch import index
from wagtail.wagtailsnippets.models import register_snippet
from djmoney.models.fields import MoneyField
from category.models import Category
from courtesy_of.models import CourtesyOf
from reggie_market.core.views import products_context
from reggie_market.core.models import TimeStampedModel, ExtraContextMixin
from users.models import add_wishlist
class ProductCategory(Orderable, models.Model):
category = ParentalKey(Category, related_name='related_products')
product = ParentalKey('product.Product', related_name='related_categories')
panels = [
FieldPanel('category'),
FieldPanel('product'),
]
class ProductIndex(ExtraContextMixin, Page):
subpage_types = ('product.Product', 'product.ProductIndex', 'category.Category',)
@property
def child_pages(self):
return Product.objects.live().child_of(self)
@property
def products(self):
products = Product.objects.filter(is_user_mode=False)
products = products.select_related(
'owner',
'image',
).order_by('-created')
return products
def get_context(self, request, courtesy_of=None, *args, **kwargs):
context = super(ProductIndex, self).get_context(request, *args, **kwargs)
search_query = request.GET.get('query', None)
order_by = request.GET.get('order_by', None)
courtesy_of = courtesy_of if courtesy_of is not None else request.GET.get('courtesy_of', None)
products = self.products
context = products_context(
request=request,
context=context,
objects=products,
courtesy_of=courtesy_of,
search_query=search_query,
order_by=order_by,
)
return context
@register_snippet
class Product(Page, TimeStampedModel):
parent_page_types = ['product.ProductIndex']
link = models.URLField(max_length=600)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
price = MoneyField(max_digits=10, decimal_places=2, default_currency='USD')
description = RichTextField('Product description')
sale_price = MoneyField(max_digits=10, decimal_places=2, default_currency='USD', null=True, blank=True)
sku = models.CharField(max_length=255, blank=True, null=True)
courtesy_ofs = ClusterTaggableManager(through='courtesy_of.ProductCoutesyOf', blank=True)
categories = models.ManyToManyField(to=Category, through='product.ProductCategory', related_name='products', blank=True)
is_user_mode = models.BooleanField(default=False)
search_fields = Page.search_fields + [
index.SearchField('description', partial_match=True, boost=1),
index.FilterField('is_user_mode'),
]
api_fields = [
APIField('title'), APIField('price'), APIField('sale_price')
]
def get_wishlists(self):
return self.wishlists.all()
def get_related_products(self):
count = settings.COUNT_OF_RELATED_PRODUCTS
products_by_tag = Product.objects.filter(
courtesy_ofs__in=self.courtesy_ofs.all(),
is_user_mode=False
).exclude(pk=self.pk).order_by('?')
products_by_category = Product.objects.filter(
categories__in=self.categories.all(),
is_user_mode=False
).exclude(pk=self.pk).order_by('?')
result_list = list(chain(products_by_tag, list(products_by_category)))
if products_by_tag.count() >= count:
related_products = products_by_tag[:count]
elif len(result_list) >= count:
related_products = result_list[:count]
else:
related_products = list(chain(result_list, Product.objects.filter(
is_user_mode=False
).exclude(pk=self.pk).order_by('?')[:count]))[:count]
return related_products
def serve(self, request, *args, **kwargs):
if request.method == 'POST':
try:
message = None
post = request.POST
product_id = int(post.get('product', 0)) if 'product' in post else None
wishlist_id = post.get('wishlist', 0) if 'wishlist' in post else None
note = post.get('note', '') if 'note' in post else None
if wishlist_id != 'new':
wishlist_id = int(wishlist_id)
if wishlist_id and product_id:
obj, created = self.wishlists.through.objects.get_or_create(
product_id=product_id, wishlist_id=wishlist_id
)
obj.note = note
obj.save()
message = 'Gift added to your want list' if created else 'Gift already in your want list'
else:
title = post.get('title') if 'title' in post else None
privacy = int(post.get('privacy', 0)) if 'privacy' in post else None
if title and privacy:
wishlist = add_wishlist(request.user, title=title, privacy=privacy, is_modify=True)
obj, created = self.wishlists.through.objects.get_or_create(
product_id=product_id, wishlist_id=wishlist.pk
)
obj.note = note
obj.save()
message = 'Gift added to your want list' if created else 'Gift already in your want list'
if message:
messages.add_message(
request, messages.SUCCESS,
message
)
except Exception as e:
pass
return super(Product, self).serve(request, *args, **kwargs)
def get_context(self, request, *args, **kwargs):
context = super(Product, self).get_context(request, *args, **kwargs)
current_user = request.user
context['related_products'] = self.get_related_products()
notes = self.wishlistproduct_set.filter(wishlist__user=current_user).values_list('note', flat=True)
context['notes'] = [i for i in notes if i] if notes.exists() else []
return context
Product.content_panels = Page.content_panels + [
ImageChooserPanel('image'),
FieldPanel('link'),
FieldPanel('price'),
FieldPanel('sale_price'),
FieldPanel('description'),
FieldPanel('sku'),
FieldPanel('courtesy_ofs'),
InlinePanel('related_categories', label='Categories'),
]
Product.promote_panels = Page.promote_panels