简体   繁体   English

从媒体文件夹中读取 csv 文件

[英]Read csv file from media folder

I uploaded my app to hosting我将我的应用程序上传到主机

I want read the csv file from media folder我想从媒体文件夹中读取 csv 文件

but display error FileNotFoundError但显示错误 FileNotFoundError

[Errno 2] No such file or directory: '/home/ed/public_html/myproject/project\media\A.csv' [Errno 2] 没有这样的文件或目录:'/home/ed/public_html/myproject/project\media\A.csv'

App before upload was working上传前的应用程序正在运行

setting.py设置.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网址.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视图.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.您的 static 和媒体根目录不正确。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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