简体   繁体   中英

Read csv file from media folder

I uploaded my app to hosting

I want read the csv file from media folder

but display error FileNotFoundError

[Errno 2] No such file or directory: '/home/ed/public_html/myproject/project\media\A.csv'

App before upload was working

setting.py

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

if DEBUG:

    STATICFILES_DIRS = ['/home/ed/public_html/myproject/project/static']

else:

   STATIC_ROOT = '/home/ed/public_html/myproject/project/static'

MEDIA_ROOT = '/home/ed/public_html/myproject/project/media'

urls.py

urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

view.py

 def test(request):
    context = {}
    if request.method == "POST":
        uploaded_file = request.FILES['document']
        print(uploaded_file)
        if uploaded_file.name.endswith('.csv'):
            #save file in media folder
            savefile = FileSystemStorage()
            name = savefile.save(uploaded_file.name, uploaded_file) #name of the file
            #know where to save file
            d = os.getcwd() #current directory of the project
            file_directory = d + '\media\\' + name
            readfile(file_directory)
            return redirect(results)
       else:
             messages.warning(request, 'File was not uploaded. Please use csv file!')

    return render(request, 'test.html', {})

Looking forward to hearing from you

your static and media root is incorrect. It is not a way to do like this.

do this:

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM