Home > Blockchain >  Django deployment on pythonanywere
Django deployment on pythonanywere

Time:03-03

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)

CodePudding user response:

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)

CodePudding user response:

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)
  • Related