【问题标题】:I need to upload templates through django admin panel which contain index file along with static files我需要通过 django 管理面板上传模板,其中包含索引文件和静态文件
【发布时间】: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


    【解决方案1】:

    为什么不这样呢?

    FileField 中的upload_to 键接受这样的函数

    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"template/theme/template{number}/{filename}"
    

    instance 参数是您的对象(主题模型) ,filename 参数是上传文件的原始文件名

    所以你的模型喜欢

    def index_path_upload(instance, filename):
      ...
    
    class themes(models.Model):
         index_file = models.filefield(upload_to=index_path_upload)
         ...
    

    编辑:

    from django.conf import settings
    import os
    
    
    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"
        path =  f"templates/theme/template{number}/{filename}"
        return os.path.join(settings.BASE_DIR, path)
    

    is 将上传到你的根目录(manage.py 在哪里)+ 路径 警告一下,这是一个很大的安全漏洞!

    【讨论】:

    • 这里的 instance 是什么,它指的是什么?我不明白你想在这里解释什么,你能详细说明一下吗?
    • 已更改 ;),抱歉我的英语很糟糕:D
    • 我明白了,我会检查一下,非常感谢您的帮助。
    • rgermain 我试过这样做,但我遇到了错误,我在我的问题中编辑了我对你的解决方案所做的事情,请你看看。让我知道我所做的有什么问题。
    • 您遇到了哪些错误?我看到 2 个错误,在函数“static_path_upload”中,您在返回后使用“ZipFile”提取 zip 文件,它可以工作,对于提取 zip,您需要在 django 保存文件后执行此操作,upload_to=funciton 是 n=only生成路径,不再,所以最好的解决方案是信号,但现在并不重要,主题模型中的第二个你有同名文件“index_file”
    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2023-03-29
    • 2013-06-18
    • 2012-12-10
    • 2012-02-28
    相关资源
    最近更新 更多