姜戈;我如何从自定义“皮肤”内部提供模板/图像/css/js?目录?

发布于 2024-11-06 15:52:16 字数 341 浏览 0 评论 0原文

我正在尝试制作一个可换肤的 Django 项目。

我遇到的问题是弄清楚如何从皮肤目录中而不是从媒体目录中提供文件,以便所有皮肤的图像/css/js 文件都可以驻留在皮肤的文件夹中)。

用户应该能够选择皮肤名称,最好仅通过更改“设置”中的 SKIN_NAME 变量(也许稍后是 .ini 文件)。所有模板/css/图像都将从该目录加载。

我想能够查看原始模板会很糟糕,所以也许它应该是皮肤文件夹内的“media”目录,其中包含子文件夹“css”,“js”和“images”,并且它将由那里。

尽管我有一些 Python 经验,但我对 Django 框架还很陌生,因此任何有关如何完成此操作的意见将不胜感激。

I'm trying to make a skinnable Django project.

What i'm having problems with is figuring out how i can serve file(s) from within the skin directory, and not from the media dir, so that all the skin's images/css/js files can reside in the skin's folder(s).

A user should be able to choose a skin name , preferably by only altering a SKIN_NAME variable in 'settings' (and maybe later an .ini file). And all templates/css/images would get loaded from this directory.

I imagine being able to view the raw templates would be bad, so perhaps it should be a 'media' directory inside the skin folder, with the subfolders 'css', 'js' and 'images' inside, and it would be served from there.

I'm pretty new to the Django framework even though i have some Python experience, so any input on how this is/can be done would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-11-13 15:52:16

首先,您应该将静态文件保存在 static 文件夹中,并且仅使用 media 来上传内容。

然后,在您的 static 文件夹中,您可以为每个皮肤创建一个文件夹,其中包含所需的所有 CSS、图像和 JS。

只需从您的皮肤模板导入以 {{ STATIC_URL }} 和您的皮肤名称为前缀的文件即可。

<link rel="stylesheet" src="{{ STATIC_URL }}name_of_your_skin/css/style.css" />

如果您的皮肤不需要单独的模板,您甚至可以这样做:

<link rel="stylesheet" src="{{ STATIC_URL }}{{ skin_name }}/css/style.css" />

First thing, you should rather keep your static files in a static folder and only use media for uploaded content.

Then within your static folder you could have a folder for each of your skin containing all the CSS, images and JS needed.

From your skin template just import the files prefixed by both {{ STATIC_URL }} and your skin name.

<link rel="stylesheet" src="{{ STATIC_URL }}name_of_your_skin/css/style.css" />

If your skins do not need a separate template you could even do :

<link rel="stylesheet" src="{{ STATIC_URL }}{{ skin_name }}/css/style.css" />
微暖i 2024-11-13 15:52:16

当用户选择“皮肤”名称时,请在设置文件中更改 MEDIA_ROOT。

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) #assuming your settings file is in your project root
TEMPLATE_NAME = 'my_template'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media", TEMPLATE_NAME)

print MEDIA_ROOT
# /path/to/project/media/my_template/

将来,如果这更改为可在管理中更改的可配置设置变量,您可能可以使用更改 MEDIA_ROOT 值的中间件获得相同的结果。

When the user selects a "skin" name, have that change MEDIA_ROOT in your settings file.

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) #assuming your settings file is in your project root
TEMPLATE_NAME = 'my_template'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media", TEMPLATE_NAME)

print MEDIA_ROOT
# /path/to/project/media/my_template/

Down the road, if this changes to a configurable settings variable that would be changed in the Admin, you could probably achieve the same result with a middleware that changes the value of MEDIA_ROOT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文