开发者

Django; How can i serve templates/images/css/js from inside custom 'skin' directory?

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.


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" />


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜