Django 在 pythonanywere 上的部署

发布于 2025-01-11 09:06:35 字数 1667 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2025-01-18 09:06:35

将这些行更改为此

file_directory = d+'\\media\\'+name
file_directory2 = d+'\\media\\'+name2

from django.conf import settings

file_directory = f"{d}{settings.MEDIA_URL}{name}"
file_directory2 = f"{d}{settings.MEDIA_URL}{name2}"

并确保您已在 settings.py 文件中指定 MEDIA_URL
像这样

MEDIA_URL = '/media/'

并将其添加到您的项目中(其中存在 settings.pyurls.py

from django.conf import settings
从 django.conf.urls.static 导入静态

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Change these lines to

file_directory = d+'\\media\\'+name
file_directory2 = d+'\\media\\'+name2

to this

from django.conf import settings

file_directory = f"{d}{settings.MEDIA_URL}{name}"
file_directory2 = f"{d}{settings.MEDIA_URL}{name2}"

and make sure you've specified MEDIA_URL in settings.py file
like this

MEDIA_URL = '/media/'

and add this inside your project(where settings.py exists) urls.py

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
深者入戏 2025-01-18 09:06:35

看来是路径问题。
我们的代码看起来像:

from pathlib import Path  # We use the *pathlib* library
BASE_DIR = Path().resolve()
NEED_DIR = os.path.join(BASE_DIR, 'fixtures')

Тогда ваш код будет выглядеть:

d = os.getcwd()
file_directory = os.path.join(d, 'media', name1)
file_directory2 = os.path.join(d, 'media', name2)

Looks like a path issue.
Our code looks something like:

from pathlib import Path  # We use the *pathlib* library
BASE_DIR = Path().resolve()
NEED_DIR = os.path.join(BASE_DIR, 'fixtures')

Тогда ваш код будет выглядеть:

d = os.getcwd()
file_directory = os.path.join(d, 'media', name1)
file_directory2 = os.path.join(d, 'media', name2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文