Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

How to connect Django media storage to Dokku persistence storage?

I deployed my Django app on server with Dokku. Staticfiles seems to be working as CSS is perfectly fine in admin dashboard, I can upload file but I cannot reach the file.

I discovered that I should be using Dokku persistence storage anyway. So I set it up (I suppose). Then I setup my Nginx. Before Nginx, Django was showing "page not found" for the file path. Now, I get "The page you were looking for doesn't exist.".

How can I correctly connect my app to persistence storage?

Edit: I manually added a file to /var/lib/dokku/data/storage/dokkuappname and was able to download. So Nginx is working but Django is not using storage folder as media folder.

Static and media settings on Django

# STATIC
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR / "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [str(APPS_DIR / "static")]
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]


# MEDIA
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR / "media")
# https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"

Nginx media.conf settings

location /media/ {
    alias /var/lib/dokku/data/storage/dokkuappname/;
}

dokku storage:list dokkuappname result

dokkuappname volume bind-mounts:
     /var/lib/dokku/data/storage/dokkuappname:/media
question from:https://stackoverflow.com/questions/65930224/how-to-connect-django-media-storage-to-dokku-persistence-storage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution

I wanted to see the path to Django's media folder. I put print(MEDIA_ROOT) in base.py to print the path during deployment. It was /app/my_app_dir/media. I realized that /app existed in Dokku documentation, but I always thought that it was a place holder like path/to/app. No, it was the path.

Dokku mount example

dokku storage:mount app-name /var/lib/dokku/data/storage/node-js-app:/app/storage

I fixed my mount command:

dokku storage:mount my_app/var/lib/dokku/data/storage/my_app:/app/my_app_dir/media

and it finally worked!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...