将上传的文件传递给模板 - Django

时间:2014-06-26 14:18:21

标签: python django

我按照此帖子的最高评论中的说明操作: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

0 个答案:

没有答案