Configure Django to use Render disk

Hi,

How do I configure Django so it uses the Render disk?

From Django, I can’t seem to write to a Render disk mounted on /var/data
“sudo chmod” is not possible, since the shell runs under Render and not root

Thanks!

Hi @frank , you won’t be able to sudo chmod the mount point for your disk (/var/data), but the mount point will already be writable. Are you able to use the disk without running sudo chmod?

Hi Dan,

Thanks again for your reply.

Manually I can, but writing failed on deploy when I tried setting /var/data/ as the BASE_DIR in Django settings.

(Trying to get Django to store and serve file uploads on Render)

Thank you!

I’m not sure what Django is doing, but if it’s trying to do something to the directory itself like the chmod rather than writing to the directory, you might need to first make some directory inside of /var/data like /var/data/django so Django can fully control that directory.

Thanks for your suggestion,

Tried using a subdirectory (/var/data/files) as the settings path for Django upload storage with no luck.

OSError: [Errno 30] Read-only file system: ‘/var/data’
Feb 18 02:46:41 PM ==> Build failed :disappointed:

Manually I can write there, but I’m not sure how to let Django do the same.
Current problem is that file uploads disappear after a new deploy because they’re not written to the separate Render disk.

Thanks for the additional logs :slight_smile: Seeing that this is occurring during the build phase makes the problem clearer to me:

Persistent disks are only mounted at runtime, not at build time. At build time, your build script should only be creating files in the current directory at the start of the build (/opt/render/project/src). The files in that directory are bundled as a single asset used to deploy to your service instances.

If you need to generate files during build-time, they should be put in that directory. If you need build-time assets to be present in your persistent disk, they must be moved during run-time, since that is when the persistent disk is present.

We also have a Getting Started with Django on Render doc that may be useful as a reference for configuration help.

One also note, that you will need to keep gunicorn in mind, it might be helpful to run gunicorn --pythonpath app app

This is a serious limitation when using SQLite as the database backend. We need to generate our migrations during build time but we want to store the database on a persistent disk.

1 Like

If you are using SQLite as database and persist to Render disk, you can create a start script and add the migration command (eg python manage.py migrate) and the program start command (eg gunicorn mysite.wsgi:application) into this script.

  1. If the migration will not take too long, these 2 commands can be executed in sequence.
  2. If the migration takes longer (ex: adding an index to a large dataset), you can configure these 2 commands to be executed in parallel. However, please make sure the program can tolerate the migration not having been completed yet. Otherwise you can commit and deploy the migration file first, then commit the changes of the program.

I wrote a python manage.py migrate && gunicorn dj_config.wsgi:application command and it works.
But I think this workaround is a little dirty and confusing, especially because it makes no sense to re-run migrations when a new instance starts.
This in another reason to add a release phase to Render in my opinion.

@ddahan

Thanks for the feedback! We do have an open request around this which you can upvote to receive future updates on https://feedback.render.com/features/p/release-phase-script

I got the files to upload to the disk, but how do I serve them? Is there any way to set up a webserver for serving media files/ I guess I can do it with static_serve from Django, but it seems to be highly inefficient, especially for large files.

Use Whitenoise to serve directly from django.

DataGreed, that’s great to hear that you were able to upload files to disk. Could you please share your Python code for doing so if that is ok. I am having the exact same problem but with Python Flask. I also want to upload files to disk then serve them from the disk. Thanks in advance to anyone who might be able to assist or point me to the documentation for doing the above in Python Flask on Render.

Hi Dan, could you please share the Python code for doing this if that is ok. I am having the exact same problem but with Python Flask. I also want to upload files to disk then serve them from the disk. Thanks in advance to anyone who might be able to assist or point me to the documentation for doing the above in Python Flask on Render.

1 Like

Is there still no solution to this? I’m having the same issue where I can’t actually use the disk for uploaded media. I’m guessing that I’ll have to use the Amazon S3 route but it seems like this should be a very doable thing.

1 Like

Did you find the solution, on render and not on amazon s3?