我刚刚开始使用 Pinax 0.9a2。基本配置,并部署了“accounts”项目。我观察到在安装的应用程序中我有“pinax_theme_bootstrap”。主页正确显示,带有基本应用程序主题的 css。如果我浏览页面源代码,在各种内容中,我有这个 css
http ://localhost:8000/site_media/static/css/bootstrap.min.css
关键是我找不到这条线的来源,也找不到它的实际服务方式。我知道它可能是由 pinax_theme_bootstrap 应用程序生成的,但我不明白:如果其中没有 urls.py 来解析上面的行并传递文件,那么这个应用程序应该如何运行?
该文件存在并且在此处具有完整路径
MacQuela:~/Work/Projects/test/test/lib/python2.7/site-packages/pinax_theme_bootstrap/static/css $ ls
bootstrap.min.css extra.css
这是静态声明时的 settings.py
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/site_media/static/"
# Additional directories which hold static files
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, "static"),
]
STATICFILES_FINDERS = [
"staticfiles.finders.FileSystemFinder",
"staticfiles.finders.AppDirectoriesFinder",
"staticfiles.finders.LegacyAppDirectoriesFinder",
"compressor.finders.CompressorFinder",
]
没有指定 pinax 可以在哪里找到该 css。
I just started using Pinax 0.9a2. Basic configuration, and deployed the "accounts" project. I observed that in the installed applications I have "pinax_theme_bootstrap". The main page correctly shows up, with the css for the basic application theme. If I browse the page source, among the various stuff, I have this for the css
http://localhost:8000/site_media/static/css/bootstrap.min.css
The point is that I can't find anywhere where this line comes from, nor how it's actually served. I understand it is probably generated by the pinax_theme_bootstrap application, but I don't understand: how is this application supposed to operate, if there's no urls.py in it to resolve the above line and deliver the file?
The file is present and in full path here
MacQuela:~/Work/Projects/test/test/lib/python2.7/site-packages/pinax_theme_bootstrap/static/css $ ls
bootstrap.min.css extra.css
This is the settings.py when it comes to static declarations
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/site_media/static/"
# Additional directories which hold static files
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, "static"),
]
STATICFILES_FINDERS = [
"staticfiles.finders.FileSystemFinder",
"staticfiles.finders.AppDirectoriesFinder",
"staticfiles.finders.LegacyAppDirectoriesFinder",
"compressor.finders.CompressorFinder",
]
Nowhere is specified where pinax can find that css.
发布评论
评论(2)
我的评论最终在你的问题下变得很长,所以我决定尝试回答一下。
bootstrap.css 包含在 pinax_theme_bootstrap 应用程序中,如您在 theme_base.html 模板中看到的那样: https://github.com/pinax/pinax-theme-bootstrap/blob/master/pinax_theme_bootstrap/templates/theme_base.html
它引用从您的设置解析的 django 静态位置。 py 选项。现在,您实际上在稍微修改过的位置看到了该文件的缩小版本,这让我相信您有另一个 django static* 应用程序可以为您执行 css/js 的缩小。最有可能的是正在压缩的
compressor
应用程序。更新 让
你感到困惑的部分实际上是 django 如何提供静态文件: https://docs.djangoproject.com/en/dev/howto/static-files/
这里没有黑魔法。它非常简单。项目中的每个应用程序都可以包含自己的 static/ 目录,其中包含该应用程序的静态媒体。您会注意到在您的 settings.py 以及上面的链接中,有 STATIC 选项和其他选项(如查找器)的组合。这些将找到每个应用程序下的静态媒体。它还允许应用程序不必知道整个绝对 URL。相反,他们可以在模板中使用 {{STATIC}} 来获取根位置。
现在,当您使用“runserver”运行开发服务器时,它将为您解析所有静态媒体。这就是 bootstrap css 被发现的原因。 Ita 在 theme_base.html 中作为静态 url 被引用。
当您准备好投入生产时,您显然将不再使用 django 开发服务器。然后,您最终将运行“./manage.pycollectstatic”之类的命令,该命令会将所有静态媒体集中到一个位置,然后您可以通过您选择的 Web 服务器进行服务器处理。我希望这能为您解决问题。最好查看有关此内容的 django 文档,因为 pinax 是 django。
My comments ended up getting long under your question so I decided to try and take a swing at an answer.
bootstrap.css is included with pinax_theme_bootstrap app, as you can see in the theme_base.html template: https://github.com/pinax/pinax-theme-bootstrap/blob/master/pinax_theme_bootstrap/templates/theme_base.html
Its referencing the django static location which is resolved from your settings.py options. Now the fact that you are actually seeing a minified version of this file in a slightly modified location leads me to believe that you have another django static* app that performs minification of css/js for you. Most likely its the
compressor
app that is minifiying.update
The part that is confusing you is actually how django serves static files: https://docs.djangoproject.com/en/dev/howto/static-files/
There is no black magic here. Its very straightforward. Every app in your project can contain its own static/ directory which will contain static media for that app. You will notice in your settings.py and also in that link above that there is a combination of the STATIC options and the other includes like the finders. These will locate the static media under each app. And it also allows apps to not have to know the entire absolute url. Instead they can use {{STATIC}} in the templates to get the root location.
Now when you run your dev server using 'runserver' it will resolve all that static media for you. This is why the bootstrap css is being found. Ita being referenced in the theme_base.html as a static url.
When you are ready for production you obviously will no longer be using the django dev server. You will then end up running a command like './manage.py collectstatic' which will round up all the static media into a single location that you can then server through your web server of choice. I hope this clears it up for you. Its better to review the django docs about this since pinax is django.
Pinax 使用 django-staticfiles提供静态文件。您可以在
settings.py
中找到STATIC_ROOT
、STATICFILES_DIRS
、STATICFILES_FINDERS
变量。在 Pinax 帐户基础项目的
url.py
中,您可以找到如下行:url(r"", include("staticfiles.urls"))
,它定义了静态文件访问。看得更清楚,
django-statifiles
覆盖了 Debug 模式下 Django 默认的runserver
命令,它重写了 Django 默认的WSGIHandler
: StatifFilesHandler。所以当你运行python manage.py runserver
并访问站点时,它会判断该请求是否是静态文件请求,如果是,则查找存储在STATICFILES_DIRS 设置(使用
staticfiles.finders.FileSystemFinder
)和每个应用程序的static
子目录(使用staticfiles.finders.AppDirectoriesFinder
)INSTALLED_APPS
。这里,
pinax_theme_bootstrap
是一个Django应用程序,仅包含static
目录下的静态文件,默认由pinax帐户基础项目安装。静态文件访问由staticfiles
应用解析,因此您在pinax_theme_bootstrap
下找不到任何urls.py
文件。当您想要将项目部署到产品环境时。您应该使用
python manage.pycollectstatic
命令来收集STATIC_ROOT
目录下的所有有用的静态文件。然后您可以通过 Nginx 或其他 http 服务器来提供此目录。当您访问 http://localhost:8000/site_media/static/css/bootstrap 时.min.css,那么Django会在
urls.py
中匹配这个url,每个特定的配置直到最后一个staticfiles
配置才会匹配。所以这个访问将通过staticfiles
来解析。根据你的STATICFILES_FINDER
配置,staticfiles
首先查找你的项目下的static
目录,如果结果为none,那么它将查找每个名为的子目录
。INSTALLED_APPS
中的 >staticPinax use django-staticfiles serve the static files. You can find the
STATIC_ROOT
,STATICFILES_DIRS
,STATICFILES_FINDERS
variables in thesettings.py
.In Pinax account base project's
url.py
, you can find the line like this:url(r"", include("staticfiles.urls"))
, it's define the static file access.Get a better view,
django-statifiles
overwrite the Django defaultrunserver
command under the Debug mode, it's rewrite the Django defualtWSGIHandler
withStatifFilesHandler
. So when you runpython manage.py runserver
and access the site, it will determine whether the request is a static file request, if true, it's find the file stored in theSTATICFILES_DIRS
setting(usingstaticfiles.finders.FileSystemFinder
) and astatic
subdirectory of each app(usingstaticfiles.finders.AppDirectoriesFinder
) inINSTALLED_APPS
.Here, the
pinax_theme_bootstrap
is a Django app only include static files understatic
directory and default installed by pinax account base project. The static files access is resolved bystaticfiles
app, so you can't find anyurls.py
file underpinax_theme_bootstrap
.When you want to deploy your project to a product environment. You should use
python manage.py collectstatic
command to collect all your useful static files under theSTATIC_ROOT
directory. And then you can serve this directory by Nginx or other http server.When you access http://localhost:8000/site_media/static/css/bootstrap.min.css, then Django will match this url in the
urls.py
, every specific config will doesn't match until the laststaticfiles
config. So this access will be resolved bystaticfiles
. According yourSTATICFILES_FINDER
config,staticfiles
find thestatic
directory under your project first, if result is none, then it will find every subdirectory namedstatic
inINSTALLED_APPS
.