[英]Django: How to set a new static path in production?
working with django 3.0.5, but i guess this relates also to < django 2.0.使用 django 3.0.5,但我想这也与 < django 2.0 有关。
I uploaded my first django app on my providers space, so far everything works.我在供应商空间上传了我的第一个 django 应用程序,到目前为止一切正常。 This is a Server Schema of my Provider...这是我的提供者的服务器架构...
_
(` ).
( ). .-------. .-------.
_( '`. ----------> | nginx | -----> | httpd |
.=(`( Internet ) '-------' '-------'
(( (..__.:'-' | => php via php-fpm
`( ) ) | => static files
` __.:' ) | => htaccess
--' |
| .--------------.
'----------> | Web Backends |
'--------------'
=> per-user nginx
=> nodejs, python, ruby, ...
=> gogs, mattermost, matrix, ...
However i am still do not understand some Django Static logics.但是我仍然不明白一些 Django Static 逻辑。 My static files are served via a separate Apache service.我的 static 文件通过单独的 Apache 服务提供。 My App called blackbird
我的应用程序叫blackbird
The following is like a web backend print下面就像一个 web 后端打印
blackbird.abc/blackbird_assets apache
blackbird.abc http:8080 Ok, listening : PID 10104, vassalsuWSGI worker 2
This is like my server account dir looks like这就像我的服务器帐户目录看起来像
User
|
'-blackbird_app
| '- manage.py
'-hmtl <symbolic link to my documentroot>
'- blackbird_assets
'- static_storage
'-production_static
'-css
'-img
If i like to rename my production_static
folder on my apache site to hello_static
and reboot my app, django did not find the static files.如果我想将我的 apache 站点上的production_static
文件夹重命名为hello_static
并重新启动我的应用程序,django 没有找到 static 文件。 Alright i guess, but on the other hand how could django even find production_static
because this are my current settings.py
好吧,我猜,但另一方面,django 怎么能找到production_static
因为这是我当前的settings.py
settings.py
Productionsettings.py
生产
DEBUG=false
INSTALLED_APPS = [
<...>
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
# STATIC_ROOT = ''
# STATICFILES_DIRS = ''
I also tried to set off the whole django.contrib.staticfiles
and django can still find production_static
without it, but not hello_static
我还尝试关闭整个django.contrib.staticfiles
和 django 仍然可以找到production_static
没有它,但不是hello_static
The only time i mention explicit static folders was in Development.我唯一一次提到明确的 static 文件夹是在开发中。 Before i collectstatic and uploaded it to my apache folder and via Git my App.在我收集静态并将其上传到我的 apache 文件夹并通过 Git 我的应用程序之前。 Sidenote: i do not have static folders in my blackbird production app folder.旁注:我的 blackbird 生产应用程序文件夹中没有 static 文件夹。
settings.py
Developmentsettings.py
开发
STATIC_URL = '/static/'
STATIC_ROOT = '/static_storage/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, env('STATICFILES_DIRS'))]
# env('STATICFILES_DIRS') = static_storage/development_static
Question问题
The Django Doc said STATIC_ROOT
is only for collectstatic. Django Doc 说STATIC_ROOT
仅用于 collectstatic。 So i use it as an export folder in dev.所以我将它用作开发中的导出文件夹。 But it seems internally a little bit more is happened?但似乎内部发生了更多事情? Otherwise Django would not look outside his Project Folder in my Production App for static_storage/development_static
but when i set my folder to static_storage/hello_static
it missed it to find.否则 Django 不会在我的生产应用程序中查看他的项目文件夹之外的static_storage/development_static
但是当我将我的文件夹设置为static_storage/hello_static
时,它找不到它。
STATICFILES_DIRS
- (additional folders) as an option - only relates in reference to collectstatic, or miss i something? STATICFILES_DIRS
- (附加文件夹)作为一个选项 - 仅与 collectstatic 相关,或者错过我的东西?
Is django saving the static folder path after collectstatic
in the project internally? django是否在项目内部保存了collectstatic
后的static文件夹路径?
Is there a way to set a new explicit static path in production for Django or do i have set alias via apache to a new static folder? Is there a way to set a new explicit static path in production for Django or do i have set alias via apache to a new static folder? I read about it, but first i have to understand the logic of django.我读过它,但首先我必须了解 django 的逻辑。
Alright, between Front and Backend i must have overlooked that my uWSGI
service for my App is mapping my static folder / STATIC_URL
.好吧,在前端和后端之间,我一定忽略了我的应用程序的uWSGI
服务正在映射我的 static 文件夹/ STATIC_URL
。 I forgot about this.我忘了这件事。
On the other hand STATIC_ROOT
is maybe semantically misleading - unlike MEDIA_ROOT
- STATIC_ROOT
is only collecting static files in a folder, when collectstatic
is executed.另一方面, STATIC_ROOT
可能在语义上具有误导性 - 与MEDIA_ROOT
不同 - STATIC_ROOT
仅在执行collectstatic
时在文件夹中收集 static 文件。 I mixed that up.我把它弄混了。 I guess something like STATIC_COLLECT
would be more obvious, to differentiate.我STATIC_COLLECT
这样的东西会更明显,以区分。
However now static and media files are working in my production.但是现在 static 和媒体文件正在我的制作中工作。
blackbird_app.ini (uWSGI) blackbird_app.ini (uWSGI)
set-placeholder = var_static=/home/user/html/blackbird_assets
<...>
static-map = /static=%(var_static)/static
static-map = /media=%(var_static)/media
<...>
Whenever Django uses STATIC_URL
or MEDIA_URL
uWSGI will map everything i put in my apache.每当 Django 使用STATIC_URL
或MEDIA_URL
时,uWSGI 将 map 我放入 apache 的所有内容。
setting.py设置.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("BASE_DIR:", BASE_DIR)
# Server Base Level or Project Dev Map
SERVER_DIR = os.path.dirname(BASE_DIR)
print("SERVER_DIR:", SERVER_DIR)
<...>
> settings.py
# --- STATIC_URL
STATIC_URL = '/static/'
# --- STATIC_ROOT
STATIC_ROOT = os.path.join(SERVER_DIR, env('STATIC_ROOT'))
print("STATIC_ROOT:", STATIC_ROOT)
# --- STATICFILES_DIRS
STATICFILES_DIRS = [os.path.join(SERVER_DIR, env('STATICFILES_DIRS'))]
print("STATICFILES_DIRS:", STATICFILES_DIRS)
# -- MEDIA_URL
MEDIA_URL = '/media/'
print("MEDIA_URL:", MEDIA_URL)
# -- MEDIA_ROOT
if DEVELOPMENT_MODE == True:
MEDIA_ROOT = os.path.join(SERVER_DIR, env('MEDIA_ROOT_DEV'))
else:
MEDIA_ROOT = env('MEDIA_ROOT_PROD')
print("MEDIA_ROOT:", MEDIA_ROOT)
print("\n")
I used that much print()
to track the issue.我用了那么多print()
来跟踪问题。 On the other hand, from now on i will use it to get a better view in my app logs另一方面,从现在开始,我将使用它来更好地查看我的应用程序日志
.env .env
i use django-environ to set my vars.我使用 django-environ 来设置我的变量。
<...>
STATICFILES_DIRS=dev_static
STATIC_ROOT=static_root
MEDIA_ROOT_DEV=media_root
MEDIA_ROOT_PROD=/var/www/virt/user_name/html/blackbird_assets/media
<...>
At first i had an Issue with MEDIA_ROOT_PROD
because i did not understand how to name the path correctly.起初我遇到了MEDIA_ROOT_PROD
的问题,因为我不明白如何正确命名路径。 My Provider for example used something like /home/virt/....
in the Doc´s also the Django Doc´s spoke about /var/www/example.com/media/
.例如,我的 Provider 在文档中使用了/home/virt/....
之类的东西,Django 文档也谈到了/var/www/example.com/media/
。 So i used the path that my SFTP und FTP Client showed me and now it works.所以我使用了我的 SFTP 和 FTP 客户端向我展示的路径,现在它可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.