pythonpython-3.xdjangodebuggingmedia-url

[django]when debug=false,MEDIA_URL returns not found


when DEBUG=TRUE,media_url is working,but DEBUG = False ,returns not working.
This is my setting file.

setting.py

DEBUG = False
...
MEDIA_URL = "/pics/"
MEDIA_ROOT = BASE_DIR

urls.py

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

home.html

...
<img src="{{ post.image.url}}" ..>

models.py

class Post(models.Model):
    title    = models.CharField(max_length=255)
    pub_date = models.DateTimeField()
    image    = models.ImageField(upload_to="media/")

maybe,this setting is recommended debug-mode.
What shuld I change this setting.


Solution

  • As per the documentation:

    This helper function works only in debug mode and only if the given prefix is local (e.g. /media/) and not a URL (e.g. http://media.example.com/).

    With the helper function they mention being: + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

    Setting up static & media files for nginx in production very simple, DigitalOcean has a great guide. The Static part is just a couple lines:

        location /media/ {
            root /home/sammy/myproject;
        }