Need help with managing media files during deployment. I've read several articles and documentation regarding to subject, but can't fix my particular site: tis, this and django documentation. Guess I've missed some obvious thing, or it could be misprint in code (link to media files).
So, I've deployed site on django, using gunicorn and nginx. When users download some picture on site it save in media directory.
models.py
class News(models.Model):
title = models.CharField(max_length=128)
content = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
photo = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True)
settings.py
DEBUG = False
...
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('news/', include('news.urls')),
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Try to add
urlpatterns = [...]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
but it doesn't work for me.
I guess it could be some problem in pass to media file.
I've checked link in page - it's correct.
<img src="photos/2022/05/10/kitten.jpg">
And this file is there.
Can't download picture on page.
How to download and use media files correct? I'll be glad to get advice or link to any documentation regarding to this subject. Thank you in advance.