Django自定义文件存储系统 [英] Django Custom File Storage system
问题描述
我有一个自定义存储空间
I have a custom storage
import os
from django.core.files.storage import Storage
class AlwaysOverwriteFileSystemStorage(Storage):
def get_available_name(self, name):
"""
Directly Returns a filename that's
from what user input.
"""
if self.exists(name):
# Remove the existing file
os.remove(name)
# Return the input name as output
return name
我想知道我应该把他的<$ c $放在哪里c> AlwaysOverwriteFileSystemStorage.py 文件以及如何通过 settings.py 定义 DEFAULT_FILE_STORAGE
I want to know where should I put his AlwaysOverwriteFileSystemStorage.py
file and how should by settings.py define DEFAULT_FILE_STORAGE
我的Django根文件夹为 / home / username / webapp
My Django root folder is /home/username/webapp
当我放入 DEFAULT_FILE_STORAGE时='site.storage.AlwaysOverwriteFileSystemStorage'
,它将返回
错误
导入存储模块site.storage时出错:
Error importing storage module site.storage: "No module named storage"
我不熟悉使用Python / Django,任何帮助将不胜感激。谢谢。
I am not familiar with Python/Django, any help will be much appreciated. Thank you.
推荐答案
您可以将其放置在任何位置,但应在settings.py
You can put it anywhere, but you should point the path to there in the settings.py
您可以将此 storage.py
文件放在根文件夹(具有管理的文件)中。 py
)并将路径指向 storage.AlwaysOverwriteFileSystemStorage
You can put this storage.py
file in the root folder (the one that has manage.py
) and point the path as storage.AlwaysOverwriteFileSystemStorage
这篇关于Django自定义文件存储系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!