我按照此帖子的最高评论中的说明操作:Need a minimal Django file upload example这是我的views.py
。我对创建文件列表不感兴趣,所以我删除了它。
def reikna(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
new_doc = Document(docfile = request.FILES['docfile'])
# Marks the file as /options/ file
new_doc.save()
# Redirect to the document list after POST
#return HttpResponseRedirect(reverse('notendur.views.options'))
else:
form = DocumentForm() # An empty, unbound form
return render('file_view2.html', {'new_doc': new_doc})
然而,当我这样做时,我得到了
UnboundLocalError: local variable 'new_doc' referenced before assignment
这是否与new_doc
的范围有关?我该怎么做?
编辑__我的模特
class Document(models.Model):
docfile = models.FileField(upload_to=_upload_path)
def get_upload_path(self,filename):
return "uploads/"+str(self.user.id) + '/' + str(date.today()) + '/' + filename