【发布时间】:2020-11-21 05:55:51
【问题描述】:
我正在尝试创建一个门户,在其中获取来自不同用户的登录页面模板并手动检查该模板是否是工作文件。
如果一切看起来都不错,那么我想从我的 django 管理面板上传该主题,该面板为每个模板创建一个新文件夹,例如在本例中它的 template1、template/theme/template1/index.html 文件夹以及我想要上传 zip 文件的静态文件对于静态文件的所有文件夹,后端会发生什么是转到文件夹static/template1/(unzipped static files),其中解压缩的静态文件是所有静态文件将自行解压缩的位置,以便我的 index.html 和静态文件可以相互通信。
我无法找到任何解决方案。
我们如何制作这样一个上传文件的模型,以便每次都创建一个新文件夹?
from django.db import models
from django.contrib.auth.models import User
from django.db import models
class themes(models.Model):
index_file = models.filefield(upload_to=???????)
static_files = models.filefield(upload_to =?????)
我也不想每次上传新模板设计时都收集静态。
在此处编辑 2 >> 在我的 models.py 中,我尝试这样做,但我遇到了错误。 ,我哪里做错了?
def index_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
filename = "index.html"
return f"templates/theme/template{number}/{filename}"
def static_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
return f"static/template{number}/+.zip"
class themes(models.Model):
index_file = models.FileField(upload_to=index_path_upload)
static_file = models.FileField(upload_to=static_path_upload)
我需要在我的设置文件中添加其他东西吗,目前这是我所拥有的。
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
MEDIA_URL = 'static/'
MEDIA_ROOT = (os.path.join(BASE_DIR,'static/'))
【问题讨论】:
标签: python-3.x django django-models django-templates django-staticfiles