U
U
Untiwe2021-04-27 18:07:35
Django
Untiwe, 2021-04-27 18:07:35

Why doesn't python create a file on the server?

I have a function. Which takes a picture, (imagefield) saddles a new one in webp format from it, returns the url of the new picture. Next, I update the picture in the model
Function code

from PIL import Image
from mysite.settings import BASE_DIR # берем адрес корня проекта из settings
import os

def compress_image(image):
    
    # filepath =file.url.replace('/','\\')# для винды
    filepath = image.url #для сервера
    picture = Image.open(str(BASE_DIR) + filepath).convert('RGB')

    photo_url = (str(BASE_DIR) + filepath).split('.')[0]
    photo_url = f'{photo_url}.webp'
    picture.save(photo_url, 'webp')
    
    #удаляем старую картинку
    os.remove(str(BASE_DIR) + filepath)

    #формируем путь нового файла для модели
    new_filepach = image.url.split('.')[0]
    new_filepach = new_filepach.replace('/media/','')
    new_filepach = f'{new_filepach}.webp'
    
    return new_filepach


On a local computer with win10, everything is ok.
On the server, the model is updated, there are no errors, but the picture itself is not saved.

I suspect I screwed up somewhere with the file path, but I only suspect. The site is hosted by regru and runs in a virtual environment, vim

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2021-04-27
@Untiwe

always look at the logs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question