我正在构建一个示例 Django 应用程序,它为 Products 及其图片收集少量输入。
我已将 Product 模型与表单集成,并尝试通过 Django admin 添加新产品...
当我尝试创建新产品时出现以下错误。
错误:
TypeError at /admin/catalog/product/add/
coercing to Unicode: need string or buffer, tuple found
Request Method:
POST
Request URL:
http://127.0.0.1:8000/admin/catalog/product/add/
Django Version:
1.4.3
Exception Type:
TypeError
Exception Value:
coercing to Unicode: need string or buffer, tuple found
Exception Location:
c:\python27\lib\ntpath.py in abspath, line 471
代码:
模型.py
class Product(models.Model):
name = models.CharField(max_length=255,unique=True)
slug = models.SlugField(max_length=255,unique=True,help_text="Unique value for product page URL, created from name.")
brand = models.CharField(max_length=50)
sku = models.CharField(max_length=50)
price = models.DecimalField(max_digits=9,decimal_places=2)
old_price = models.DecimalField(max_digits=9,decimal_places=2,blank=True,default=0.00)
#image = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
is_bestseller = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
quantity = models.IntegerField()
description = models.TextField()
meta_keywords = models.CharField(max_length=255,help_text="Comma-delimited set of SEO Keywords for meta tag")
meta_description = models.CharField(max_length=255,help_text="Content for description meta tag")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField(Category)
image = models.ImageField(upload_to='images/products/main')
thumbnail = models.ImageField(upload_to='images/products/thumbnails')
image_caption = models.CharField(max_length=200)
class Meta:
db_table = 'products'
ordering = ['-created_at']
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_product',(),{'product_slug':self.slug})
@property
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
设置.py
MEDIA_ROOT = "static/",
请求对象:
GET
No GET data
POST
Variable
Value
sku u'239'
meta_description u'iphone'
meta_keywords u'Iphone'
name u'Apple iphone'
_save u'Save'
is_active u'on'
price u'239'
description u'Iphone'
old_price u'200'
slug u'apple_iphone'
image_caption u'phone'
csrfmiddlewaretoken u'SuO3uQ5SFZ0bo153CXIcIXPcmnv5hHsn'
brand u'apple'
categories u'1'
quantity u'1'
FILES
Variable
Value
image <InMemoryUploadedFile: snip.PNG (image/png)>
thumbnail <InMemoryUploadedFile: snip.PNG (image/png)>
在 Image 对象中上传 png 文件是否有问题?