1

Whenever I am calling the uploaded_file_url in django,it shows

IOError at /home/ [Errno 2] No such file or directory: '/media/sample_6bYaPzY.py'

But I checked manually.The directory and file are exist

Here is my code

uploaded_file_url=None
uploaded_file_url2=None
def upload(request):
    if request.method == 'POST':
        #request.FILES['myfile'] and request.FILES["myfile1"]:
        myfile=request.FILES['myfile']
        myfile2=request.FILES["myfile2"]
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        filename2=fs.save(myfile2.name, myfile2)
        global uploaded_file_url
        uploaded_file_url = fs.url(filename)
        global uploaded_file_url2
        uploaded_file_url2 = fs.url(filename2)
        return HttpResponseRedirect('/home/')


        #upload_file = request.FILES['upload_file']
        #data = [row for row in csv.reader(upload_file.read().splitlines())]
    return render(request, 'myapp/upload.html')


def home(request):
    data= open(os.path.join(settings.MEDIA_ROOT, str(uploaded_file_url)),'rb').read()
    //some code here
    return render(request,'myapp/home.html',{"a":a})

settings cofiguration

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Here I want to open the uploaded_file . Any suggestions here.Thanks in advance

  • Post full error message with traceback please. – Selcuk Apr 26 '18 at 10:45
  • Have you added `django.views.static.serve` to your urlpatterns? See https://stackoverflow.com/a/5518073/1472458 – art Apr 26 '18 at 11:19
  • Thanks for your suggestion@art06. But here I dont want file path in my urls. I just want to read the file – Dinesh Kumar Apr 26 '18 at 11:34
  • Are you sure you have that directory? Note that the error message starts with a `/` meaning that it is a top level directory on your file system. – Selcuk Apr 30 '18 at 03:39

0 Answers0