Django 在 pythonanywere 上的部署
我在 pythonanywhere 上部署了 django 应用程序 但我发现相对路径有问题。
这个应用程序可以上传并保存2个文件在一个目录中 我的代码实际上是:
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
import pandas as pd
import datetime
from datetime import datetime as td
import os
from collections import defaultdict
def home(request):
#upload file and save it in media folder
if request.method == 'POST':
uploaded_file = request.FILES['document']
uploaded_file2 = request.FILES['document2']
if uploaded_file.name.endswith('.xls'):
savefile = FileSystemStorage()
#save files
name = savefile.save(uploaded_file.name, uploaded_file)
name2 = savefile.save(uploaded_file2.name, uploaded_file2)
d = os.getcwd()
file_directory = d+'\\media\\'+name
file_directory2 = d+'\\media\\'+name2
results =results1(file_directory,file_directory2)
我给出了这个错误
FileNotFoundError at /
[Errno 2] No such file or directory: '\\Listing
/home/VIRAD/Django1/viruss/views.py, line 26, in home
results,output_df,new =results1(file_directory,file_directory2)
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("v.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I did the deployment of my django app on pythonanywhere
but i see that i have a problem with relative path .
this app can upload and save 2 files in a directory
my code actually is:
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
import pandas as pd
import datetime
from datetime import datetime as td
import os
from collections import defaultdict
def home(request):
#upload file and save it in media folder
if request.method == 'POST':
uploaded_file = request.FILES['document']
uploaded_file2 = request.FILES['document2']
if uploaded_file.name.endswith('.xls'):
savefile = FileSystemStorage()
#save files
name = savefile.save(uploaded_file.name, uploaded_file)
name2 = savefile.save(uploaded_file2.name, uploaded_file2)
d = os.getcwd()
file_directory = d+'\\media\\'+name
file_directory2 = d+'\\media\\'+name2
results =results1(file_directory,file_directory2)
i gived this error
FileNotFoundError at /
[Errno 2] No such file or directory: '\\Listing
/home/VIRAD/Django1/viruss/views.py, line 26, in home
results,output_df,new =results1(file_directory,file_directory2)
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("v.urls")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将这些行更改为此
,
并确保您已在 settings.py 文件中指定
MEDIA_URL
像这样
并将其添加到您的项目中(其中存在 settings.py) urls.py
from django.conf import settings
从 django.conf.urls.static 导入静态
Change these lines to
to this
and make sure you've specified
MEDIA_URL
in settings.py filelike this
and add this inside your project(where settings.py exists) urls.py
from django.conf import settings
from django.conf.urls.static import static
看来是路径问题。
我们的代码看起来像:
Тогда ваш код будет выглядеть:
Looks like a path issue.
Our code looks something like:
Тогда ваш код будет выглядеть: