0
收到以下錯誤試圖創建(&保存)與文件中的字段一個Django模型的Django模型:在/管理/ APP_NAME /模板類型的錯誤,同時創建具有文件場
類型錯誤/添加/
脅迫到Unicode:需要字符串或緩衝區,詮釋發現
Request Method: POST
Request URL: http://localhost:8000/admin/app_name/template/add/
Django Version: 1.4.3
Exception Type: TypeError
Exception Value: coercing to Unicode: need string or buffer, int found
Exception Location: path_to_python\python\lib\site-packages\django\utils\encoding.py
in force_unicode, line 71
這裏是models.py: -
class Template(models.Model):
title = models.CharField(max_length=300, unique=True)
template = models.FileField(upload_to='templates')
def __unicode__(self):
return self.title
class TemplateAdmin(admin.ModelAdmin):
def upload_file(request,*args, **kwargs):
if request.method == 'POST':
instance = Template(template=request.FILES['template'])
instance.title =request.POST['title']
instance.save()
admin.site.register(Template, TemplateAdmin)
這裏是settings.py:-
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
此外,雖然有試圖挽救(創建)一個模板對象時,這個錯誤,仍該文件似乎被上載到指定的目錄中(因爲我可以在該目錄中找到它)...但是沒有創建對象實例(因爲Template.objects.all()
返回空列表)。