如何在 django nonrel 中使用静态文件
我正在尝试将 Django nonrel 项目用于谷歌应用程序引擎。我按照此处所述设置测试项目。我在项目中添加了一个名为“static”的新文件夹来存放静态文件。对于 app.yaml 文件,我添加了以下行;
- url: /static
static_dir: static
我无法访问我的静态文件。我需要做额外的配置吗?
提前谢谢。
I'm trying to use the Django nonrel project for google app engine. I setup the test project as described here. I added a new folder to the project named "static" for my static files. And for the app.yaml file i added the lines;
- url: /static
static_dir: static
I can't reach my static files. Do i have to do additional configuration?
Thx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如人们已经指出的那样,您应该将
static_dir
指令放在/.*
模式之前。但是,这并不是您应该了解的唯一事情。
通过将此指令放入 app.yaml 中,您可以使 AppEngine Web 服务器(无论是开发服务器还是生产服务器)处理路径
/static
,并且您需要所有静态文件都位于 static 目录中。这意味着每次更改静态文件中的任何内容时(特别是如果您拥有/使用带有静态文件的应用程序 - 例如,例如,admin<),您都必须运行 python manage.pycollectstatic /code> 或 django-tinymce)只是为了在本地服务器上测试这些更改那么如何避免这种情况呢?默认情况下,staticfiles 提供帮助程序在开发服务器上提供这些文件,而无需每次都运行collectstatic,问题是上一段中描述的目录名称冲突:Django 无法捕获对静态文件路径的请求,因为它们是由 appserver 处理的。您可以通过在开发和生产服务器上使用不同的路径来解决它:
(djangoappengine 在开发服务器上将 DEBUG 设置为 True)。您可以保留
ADMIN_MEDIA_PREFIX = '/static/admin/'
,但记得在使用 admin 之前至少运行一次collectstatic当然记得使用
{{ STATIC_URL }}path/to.css<模板中的 /code> 而不是
/static/path/to.css
哦,我假设您区分了您处理的原始静态文件的目录和应该收集静态文件的目录。我在 settings.py 中使用它:
这意味着:您将静态字段放入
static
目录(以及应用程序的static
目录),collectstatic 将它们收集到
sitestatic
目录中。适当的app.yaml
指令是最后,您可以配置
app.yaml
在上传时忽略static
和media
目录您的应用程序,因为所有静态文件都将被收集到sitestatic
中并从中提供服务。但是,您应该仅在上传时设置此项(否则这些文件将在调试服务器中不可用)As people already pointed out, you should put your
static_dir
directive before/.*
patternHowever, that is not the only thing you should know about.
By putting this directive into app.yaml, you make AppEngine webserver (whether it's development or production server) handle the path
/static
, and you need all the static files to be inside static directory. This means you will have to runpython manage.py collectstatic
every time you change anything in your static files (especially if you have/use apps with static files -- like, say,admin
ordjango-tinymce
) just to test these changes on local serverSo how to avoid that? By default staticfiles provides helpers to serve these files on development server without running collectstatic every time, the problem is the direcotry name conflict described in the previous paragraph: Django can't catch requests to your static files path, as they are handled by appserver. You can resolve it by using different paths on development and production server:
(djangoappengine sets DEBUG to True on development server). You can leave
ADMIN_MEDIA_PREFIX = '/static/admin/'
, but remember to run collectstatic at least once before using adminOf course remember to use
{{ STATIC_URL }}path/to.css
in templates instead of/static/path/to.css
Oh, and I assume that you distinguish the directory for original static files you work on and the directory where static files should be collected. I use this in my settings.py:
This means: you put your static fiels into
static
dir (and into your apps'static
dirs),collectstatic
collects them intositestatic
dir. Appropriateapp.yaml
directive isFinally, you can configure
app.yaml
to ignorestatic
andmedia
directories when uploading your app, since all the static files will be collected into and served fromsitestatic
. However, you should set this only while uploading (otherwise these files will not be available in debug server)app.yaml
与 Django 无关,但它确实配置了 App Engine 前端。答案取决于您是否想使用 Django 还是前端(这是,更便宜且更快)来提供静态文件。如果您刚刚将
- url: /static
映射“添加”到末尾,请将其移至/.*
通配符之前。由于所有映射都是从上到下处理的 - 第一个匹配的映射获胜。app.yaml
have nothing to do with Django, but it does configures App Engine front-end. The answer depends on whether you want to serve static files with Django or the front-end (which is, well, cheaper and faster).If you just "added" your
- url: /static
mapping to the end, move it before the/.*
wildcard. As all mappings processed from top to bottom — first matching mapping wins.好吧,我刚刚想通了。只需在 main.py 之前使用 static_dir 行即可。所以 app.yaml 应该是这样的;
Well i just figured it out. Just use static_dir line before the main.py. So the app.yaml should look like this;