0

I was following the instructions from the top comment of this post: Need a minimal Django file upload example and this is my views.py. I'm not interested in creating a list of files, so I removed that.

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})

However, when I do this, I get

UnboundLocalError: local variable 'new_doc' referenced before assignment

Does this have something to do with the scope of new_doc? How should I do this?

EDIT __ My model

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
Community
  • 1
  • 1
KSHMR
  • 741
  • 1
  • 9
  • 24

0 Answers0