在Django项目/应用程序中如何组织和加载CSS? [英] How to organize and load CSS within Django project/app?
问题描述
项目设置:
项目
-myapp
-model,查看和测试
-template
- base.html
- index.html
- view1
- view2
- media
- css
- style.css
- ie.css
- js
- img
-setting.py
-url.py
我的static_url,static_root,media_url,media_root,STATICFILES_DIRS和STATICFILES_FINDERS应该怎么样?
我当前加载我的css文件是通过以下的base.html:
{%load staticfiles% }
< link rel =stylesheettype =text / csshref ={%static user_stylesheet%}type =text / cssmedia =screen/>
那么什么是管理我的静态文件的最好和最有效/正确的方式?
感谢大家的帮助。我很感激你的时间和精力。
静态文件是一个简单的过程:
-
Django将在
INSTALLED_APPS 中的任何应用程序中搜索名为
static /
的目录/ code>为静态文件。如果您的文件没有绑定到任何应用程序,您可以将它们放在单独的目录中。该目录应该添加到STATICFILES_DIRS
(一个元组),所以django知道它。在你的情况下,你应该有STATICFILES_DIRS =('/ home / user / Project / template / media',)
-
将
django.contrib.staticfiles
添加到您的INSTALLED_APPS
现在,如果您使用 runserver
进行测试,请打开您的 urls.py
并将django.contrib.staticfiles.urls中的 import staticfiles_urlpatterns
添加到顶部,并在所有URL定义完毕后,在文件末尾,添加 urlpatterns + = staticfiles_urlpatterns()
您已经有正确的方法访问您的问题中的静态文件。另一种方法是使用 {{STATIC_URL}}
,并确保使用 RequestContext
发送回复。如果您使用基于类的视图,则不必更改任何内容。对于其他正常的视图方法,只需使用 呈现
快捷方式。
STATIC_URL
和 STATIC_ROOT
在部署时派上用场。
-
STATIC_URL
是您的Web服务器已配置的URL前缀指向系统中具有静态文件的位置。这是一个 url组件,而必须以斜线结尾。例如/ static /
-
STATIC_ROOT
这是系统上目录的路径。当您准备好部署时,运行collectstatic
命令,django将找到所有静态文件,并将它们转储到STATIC_ROOT
。然后,您可以将此目录放在您的Web服务器配置的DOCUMENT_ROOT
文件夹中。然后,将/ static /
URL指向此文件夹。例如STATIC_ROOT = / home / user / project / www /
如果Apache配置了 DOCUMENT_ROOT
/ var / www /
,而您的 STATIC_URL
设置为 / static /
;运行 collectstatic
后,将文件夹 / home / user / project / www /
的内容移动到的/ var / WWW /静态/
。
-
STATICFILES_FINDERS
。此设置列出了运行collectstatic
时django可用于搜索静态文件的方法,通常您根本不会修改此选项。
-
MEDIA_ROOT
,MEDIA_URL
。这些设置控制文件系统位置,以及使用django 上传的任何文件的URL前缀;这些 不能与STATICFILES_DIRS
相同。您需要手动处理,因为collectstatic
不会触及这些位置。如果您正在开发中测试文件上传,可以使用django.views.static.serve()
从MEDIA *
。
I'm new to Django. I read the Documentation and I am literally confused with media root files and static root files and where I should be placing my css files along with my javascript files.
Can you guys point me in the correct direction? Project setup:
Project
-myapp
-model,view,and test
-template
- base.html
- index.html
- view1
- view2
- media
- css
- style.css
- ie.css
- js
- img
-setting.py
-url.py
What should my static_url, static_root, media_url , media_root, STATICFILES_DIRS and STATICFILES_FINDERS look like?
How I currently load my css file is through the base.html with the following:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static user_stylesheet %}"type="text/css" media="screen"/>
So what is the best and most efficient/correct way of managing my static files? Thank you everybody for your help. I appreciate your time and effort.
Static files are a simple process:
Django will search for a directory named
static/
inside any app that is inINSTALLED_APPS
for static files. If you have files that are not tied to any app, you can put them in a separate directory. This directory should be added toSTATICFILES_DIRS
(a tuple) so django is aware of it. In your case you should haveSTATICFILES_DIRS = ('/home/user/Project/template/media',)
Add
django.contrib.staticfiles
to yourINSTALLED_APPS
Now, if you are testing this with runserver
, open your urls.py
and add from django.contrib.staticfiles.urls import staticfiles_urlpatterns
to the top, and after all your URLs are defined, at the end of the file, add urlpatterns += staticfiles_urlpatterns()
You already have the correct method of accessing static files in your question. The other way is to use {{ STATIC_URL }}
, and make sure you are sending your responses with RequestContext
. If you are using class based views, you don't have to change anything. For your other normal view methods, just use the render
shortcut.
STATIC_URL
and STATIC_ROOT
come in handy when you are deploying.
STATIC_URL
is the URL prefix that your web server has been configured with to point to the location in the system that has your static files. This is a url component and must end in a slash. For example/static/
STATIC_ROOT
this is a path to a directory on your system. When you are ready to deploy, run thecollectstatic
command and django will find all your static files and dump them in the directory pointed to bySTATIC_ROOT
. Then you can take this directory, put it in yourDOCUMENT_ROOT
folder that your web server is configured with. Then, point the/static/
URL to this folder. Example isSTATIC_ROOT = /home/user/project/www/
If Apache is configured with a DOCUMENT_ROOT
of /var/www/
, and your STATIC_URL
setting is /static/
; after you run collectstatic
, move the contents of the folder /home/user/project/www/
to /var/www/static/
.
STATICFILES_FINDERS
. This setting lists methods that django can use to search for your static files when you runcollectstatic
, and you normally don't modify this at all.
MEDIA_ROOT
,MEDIA_URL
. These settings control the file system location, and the URL prefix of any files that are uploaded using django; these cannot be the same asSTATICFILES_DIRS
. You need to handle this manually ascollectstatic
will not touch these locations. If you are testing file uploads in development, you can usedjango.views.static.serve()
to serve files fromMEDIA*
.
这篇关于在Django项目/应用程序中如何组织和加载CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!