Where to put a Django template's dependent files?

落花浮王杯 提交于 2020-01-02 05:15:14

问题


My Django templates use a lot of related stuff: images, style sheets, etc.

Where should I put these file, or how should I refer to them in the template itself?

For now I'm using the development server.

I know it's a really common thing, but I can't really figure it out.


回答1:


I put them inside a folder named static, which is in the web project's top level folder.

Example:

/static/img/
/static/js/
/static/css/
/templates/
urls.py
settings.py

I then have the following rule in my urls.py file:

(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),

My settings.py contains:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static').replace('\\', '/')
ADMIN_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/admin').replace('\\', '/')



回答2:


Maybe you can read the doc http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files




回答3:


We put ours under /media. Everything that is specifically tied to the layout of the sites is further separated. Of course none of this static content is served by Django on the production site. They often aren't even on the same physical server.

/media
    /images - this is for content-specific images
    /video - these next 2 are normally symlinks to a /big_content folder ...
    /audio - so that they aren't included in our mercurial repository.
    /layout - everything that is tied to the basic templates.
        /css
        /js
        /images


来源:https://stackoverflow.com/questions/1891884/where-to-put-a-django-templates-dependent-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!