Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 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 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...