使用dokku部署Django应用程序时出现Collectstatic配置错误

时间:2022-05-21 19:20:10

When deploying a Django app using dokku I am getting a following error

使用dokku部署Django应用程序时出现以下错误

Collectstatic configuration error. To debug, run:
$ heroku run python ./manage.py collectstatic --noinput

I found no way to run heroku run python ./manage.py collectstatic --noinput for a dokku container, but when I am trying dokku run my app python ./manage.py collectstatic --noinput, the static files are successfully copied to the STATIC_ROOT folder and no error message given.

我发现无法运行heroku运行python ./manage.py collectstatic --noinput为dokku容器,但当我尝试dokku运行我的应用程序python ./manage.py collectstatic --noinput时,静态文件成功复制到STATIC_ROOT文件夹,没有给出错误消息。

I could solve the problem by placing collectstatic command into Procfile:

我可以通过将collectstatic命令放入Procfile来解决问题:

web: python manage.py collectstatic --noinput ; gunicorn myapp.wsgi

Still, I would love to know what was causing the problem and how can it be debugged. Any ideas?

不过,我很想知道导致问题的原因以及如何进行调试。有任何想法吗?

1 个解决方案

#1


8  

You should have four settings in your settings.py file called MEDIA_ROOT, MEDIA_URL, STATIC_ROOT and STATIC_URL.

您应该在settings.py文件中有四个名为MEDIA_ROOT,MEDIA_URL,STATIC_ROOT和STATIC_URL的设置。

I set mine like so:

我这样设置我的:

MEDIA_ROOT = 'media' STATIC_ROOT = 'static' MEDIA_URL = '/media' STATIC_URL = '/static'

MEDIA_ROOT ='media'STATIC_ROOT ='static'S MEDIA_URL ='/ media'STATIC_URL ='/ static'

Inside the docker container that gets created, you will find your application under /app which makes the media path /app/media/ and the static path /app/static/.

在创建的docker容器内,您将在/ app下找到您的应用程序,它将生成媒体路径/ app / media /和静态路径/ app / static /。

Unfortunately if you don't have a media and static folder committed in git, it won't get created under /app automatically.

不幸的是,如果你没有在git中提交媒体和静态文件夹,它将不会自动在/ app下创建。

Since git doesn't allow you to commit an empty folder (it only commits files), I do the following in my projects:

由于git不允许你提交一个空文件夹(它只提交文件),我在我的项目中执行以下操作:

mkdir media static touch media/.dir touch static/.dir git add media/.dir static/.dir git commit -m 'Make media and static directories'

mkdir media静态触摸媒体/ .dir touch static / .dir git add media / .dir static / .dir git commit -m'制作媒体和静态目录'

The 'touch' command creates an empty file, then you 'git add' the two newly-created files and check them in.

'touch'命令创建一个空文件,然后你'添加'两个新创建的文件并检查它们。

Now when you push, the directories will be there to contain the media and static files. Just keep in mind that every time you 'git push', a new container is created, and the old one is destroyed. While this isn't a problem for your static files, your media will be lost unless you store it somewhere else.

现在,当您推送时,目录将包含媒体和静态文件。请记住,每次“git push”时,都会创建一个新容器,旧容器将被销毁。虽然这对您的静态文件不是问题,但除非您将其存储在其他位置,否则您的媒体将会丢失。

#1


8  

You should have four settings in your settings.py file called MEDIA_ROOT, MEDIA_URL, STATIC_ROOT and STATIC_URL.

您应该在settings.py文件中有四个名为MEDIA_ROOT,MEDIA_URL,STATIC_ROOT和STATIC_URL的设置。

I set mine like so:

我这样设置我的:

MEDIA_ROOT = 'media' STATIC_ROOT = 'static' MEDIA_URL = '/media' STATIC_URL = '/static'

MEDIA_ROOT ='media'STATIC_ROOT ='static'S MEDIA_URL ='/ media'STATIC_URL ='/ static'

Inside the docker container that gets created, you will find your application under /app which makes the media path /app/media/ and the static path /app/static/.

在创建的docker容器内,您将在/ app下找到您的应用程序,它将生成媒体路径/ app / media /和静态路径/ app / static /。

Unfortunately if you don't have a media and static folder committed in git, it won't get created under /app automatically.

不幸的是,如果你没有在git中提交媒体和静态文件夹,它将不会自动在/ app下创建。

Since git doesn't allow you to commit an empty folder (it only commits files), I do the following in my projects:

由于git不允许你提交一个空文件夹(它只提交文件),我在我的项目中执行以下操作:

mkdir media static touch media/.dir touch static/.dir git add media/.dir static/.dir git commit -m 'Make media and static directories'

mkdir media静态触摸媒体/ .dir touch static / .dir git add media / .dir static / .dir git commit -m'制作媒体和静态目录'

The 'touch' command creates an empty file, then you 'git add' the two newly-created files and check them in.

'touch'命令创建一个空文件,然后你'添加'两个新创建的文件并检查它们。

Now when you push, the directories will be there to contain the media and static files. Just keep in mind that every time you 'git push', a new container is created, and the old one is destroyed. While this isn't a problem for your static files, your media will be lost unless you store it somewhere else.

现在,当您推送时,目录将包含媒体和静态文件。请记住,每次“git push”时,都会创建一个新容器,旧容器将被销毁。虽然这对您的静态文件不是问题,但除非您将其存储在其他位置,否则您的媒体将会丢失。