2

I have a Django app running on a server and I would like to continue working on it locally with the runserver command. My problem concern the use of common static and media files. The media folder content changing frequently in production I have to download those media files from the server each time I want to dev locally and add those to the MEDIA_ROOT path on my computer.

The database is common to both dev and production environment since it is a MySQL host on my server.

How can I tell Django in local mode to lookup static and media files on a remote url from my domain name instead of localhost? Like https://example.com/media/ instead of : https://127.0.0.1:8000/media/

EDIT : module django-storages

After some time I finally find a module which seems aligned with what I'm looking for, django-storages with its 'FTP' functionnality' however the documentation is really weak and I can't find any tutorial explaining what I want to achieve.

DEFAULT_FILE_STORAGE = 'storages.backends.ftp.FTPStorage'
FTP_STORAGE_LOCATION = 'ftp://user:password@host:21/media'

What kind of configuration should I add to my settings file to get MEDIA_ROOT looking for this location when I'm working locally ?

2 Answers 2

1

I don't really understand why static files have to be treated the same - they are supposed to be a part of your project sources. So local storage should have a higher priority.

But serving any of such files is still just URL dispatching, so instead of this:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

you might try something like this:

urlpatterns += [
    path(settings.MEDIA_URL + "<path:file_path>", RedirectView.as_view(url="https://example.com/media/%(file_path)"))
]

(did not test it)

RedirectView docs almost similar question

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, I tried your solution and some variant and unfortunately it doesn't work :/ . Additional information all files path in DB already start with 'media/'.
it doesn't work, it did not help do not help helping you. Please update your question with modified urls.py, show your settings.py and any template referencing those images. And add error details.
0

You simply test in after main url

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                  document_root=settings.STATIC_ROOT)
else:
   Production
  

1 Comment

Thanks for your message, however it is not my question what I want to know is how to tell Django to get a media with its url/path on an existing url on the internet instead of looking in my local media dir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.