请在我的回答被标记为重复之前,如果有人先通过它,我将不胜感激。 我试图访问电子商务项目中的产品页面,但当我尝试访问该页面时,我收到如下错误值:
Internal Server Error: /product_view/1/
Traceback (most recent call last):
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/agozie/shoppingmall/shop/views.py", line 15, in product_view
p = get_object_or_404(Product, product_slug)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/shortcuts.py", line 85, in get_object_or_404
return queryset.get(*args, **kwargs)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/query.py", line 371, in get
clone = self.filter(*args, **kwargs)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/agozie/anaconda3/envs/env1/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1151, in build_filter
arg, value = filter_expr
ValueError: need more than 1 value to unpack
[13/Aug/2017 19:07:23] "GET /product_view/1/ HTTP/1.1" 500 104231
我已经解决了与我有关的所有其他问题,但它们都与拆分代码有关。
这是我的models.py:
class Product(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True)
color_id = models.ManyToManyField(Color)
gender = models.CharField(max_length=2,choices=(('M','Male'),('F','Female')))
brand_id = models.ManyToManyField(Brand)
image = models.ImageField(upload_to='product_images',blank=False)
features = models.TextField()
quantity = models.IntegerField(default=0)
style = models.ManyToManyField(Style)
sub_category_id = models.ForeignKey(Sub_Category)
old_price = models.DecimalField(max_digits=15, decimal_places=2, blank=True, default=0.00)
price = models.DecimalField(max_digits=15,decimal_places=2, null=False)
availability = models.BooleanField(default=False)
retailer_id = models.ForeignKey(Retailer)
shipping_cost= models.IntegerField(default=1000)
categories = models.ManyToManyField(Category)
created_at = models.DateTimeField(auto_now=True)
def save(self, *args, **kwargs):
# self.author = self.author.upper()
self.slug = slugify(self.name)
super(Product, self).save(*args,**kwargs)
def __str__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return('shop_product', (), {'product_slug':'self.slug'})
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
以下是我的views.py:
def product_view(request, product_slug):
p = get_object_or_404(Product, product_slug)
categories = p.categories.filter(is_active=True)
return render(request,'shop/product_view.html')
我正在使用Django 1.11和Python 2.7.13
答案 0 :(得分:0)
我认为您正在尝试在网址中找到Product
的{{1}},对吧?
product_slug
是查找由其字段过滤的对象的正确方法。
p = get_object_or_404(Product, slug=product_slug)
要求您传递除类模型以外的任何参数,以便get_object_or_404
或get()
方法接受。
答案 1 :(得分:0)
如the docs所述,get_object_or_404
接受klass
作为第一个参数,然后*args, **kwargs
(其中klass
和kwargs
需要)
因此,当您调用此函数时,您需要指定keyword arguments
来获取:
p = get_object_or_404(Product, slug=product_slug)
因为Django不知道通过哪个字段获取您的Product
模型,这就是您收到ValueError: need more than 1 value to unpack
的原因。