2
當嘗試一個簡單的圖片上傳我得到一個錯誤:(!如果你需要更多的,讓我知道)Django的基於類的意見和簡單的圖片上傳
TypeError at /upload/
__init__() got an unexpected keyword argument 'instance'
下面是相關的代碼的snippits :
# Models
class Photo(models.Model):
created_at = models.DateTimeField(auto_now_add=True, editable=False)
title = models.CharField(max_length=255)
image = models.ImageField(upload_to='photos/'+str(uuid.uuid4())+'/')
# Views
class PhotoUploadView(CreateView):
model = Photo
form_class = PhotoUploadForm
template_name = 'upload.html'
success_url = '/thanks/'
# Forms
class PhotoUploadForm(forms.Form):
image = forms.ImageField()
title = forms.CharField(max_length=255)
# Urls
urlpatterns = patterns('',
url(r'^upload/', views.PhotoUploadView.as_view()),
)
剛我確定我明白。 上面的代碼屬於models.py文件,我應該從視圖中刪除model = Photo? – moreisee
啊,感謝編輯!說得通。 – moreisee
最佳做法是創建一個forms.py文件,並將所有表單放在那裏。查看更新的答案。 –