Django Media File is not found on production (404) - django

I have wondered around lot of other stackoverflow issues to see if there is a solution but i couldn't work around it. So here is the issue:
Nginx.conf
location /media/ {
alias /root/server/media/;
}
location /static/ {
alias /root/server/static/;
}
urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static/'),)
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
wsgi.py
application = WhiteNoise(application, root=os.path.join(BASE_DIR, 'static'))
application = WhiteNoise(application, root=os.path.join(BASE_DIR, 'media'))
I have set everything as necessary, i check the directory (root/server/media) and images are there. But when i enter the link : $serverlink/media/images/1.png , it gives me 404 (Not found) error. Does anybody know why?
Update:

Can you try removing the trailing slash after media?
location /media { alias /root/server/media; }

Related

i can't use static files from my Django app

I went to this route http://127.0.0.1:8000/static/corefiles/chatbot-data.xlsx but i get Page not found (404) error...
I added those to the URLs:
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
File directory is /rootproject/core/static/corefiles/testexel.xlsx
My settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(BASE_DIR, 'static')),
normpath(join(BASE_DIR, 'upload')), )
STATIC_ROOT = normpath(join(BASE_DIR, 'assets'))
Static files are to be used in your project, if you need to view a document at the specified url, you need to specify media url and media root in the settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Also add those to the URLs:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django doesn't load images from media

I've set things for the setting and the URLs as follow but it doesn't load the images from media directory:
settings.py:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')
urls.py:
urlpatterns = [
path('create/', views.image_create, name='create'),
path('detail/<int:id>/<slug:slug>/', views.image_detail, name='detail'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
this is the link generated for the page: https://127.0.0.1:8000/images/detail/1/django-and-duke/
and the link for the image: https://127.0.0.1:8000/media/images/2020/08/02/django-and-duke.jpg
I have another app named accounts and if I add the base URL of account to the first of this media URLs it works! but I know they are separated from each other.
if you need other parts of code please tell me.
Remove the slashes in os.path.join(BASE_DIR, '/media/')
If any argument to os.path.join begins with a slash / it will overwrite all previous arguments. You're currently setting MEDIA_ROOT to be /media/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

how to serve media files in django app nginx server?

I am testing my django app in production mode (debug=false) using nginx, gunicorn, postgresql.
Though I am able to render static files, I am unable to access files stored in 'media' folder.
In my settings.py following are the variables set:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# also tried another combination: MEDIA_ROOT = 'media'
Also in urls.py the MEDIA_ROOT settings are as follows:
urlpatterns = [
path('admin/', admin.site.urls),
path('venter/', include('appname.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And in my /etc/nginx/sites-available/ file I have the following settings:
server {
listen 80;
server_name website.com www.website.com ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/a/btawebsite;
}
location /media/ {
root /home/a/btawebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/a/myproject.sock;
}
}
However while rendering the xlsx file stored in my django database as follows it throws me NOT found error.
{{file.output_file.url}}
I have tried every combination of configurations required for rendering MEDIA files but unable to achieve the outcome.
Thanks.
UPDATE: following changes to be made in settings.py
MEDIA_URL = '/'
MEDIA_ROOT = 'media'
If everything in django settings is properly configured you just need to add the following in the nginx conf:
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
In your settings.py write like this
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # root for media files
MEDIA_URL = "/media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
FORCE_SERVE_STATIC = True
DEBUG=False
In your urls.py change like this
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
elif getattr(settings, 'FORCE_SERVE_STATIC', False):
settings.DEBUG = True
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.DEBUG = False
In your nginx.conf file change the root to your media folder
location /media/ {
root /home/nazmi/workspace/portal/media/ (url for your media folder);
}
First of all, remove the +static() from your urls.py. That's not correct for production, only for development.
In your nginx configuration, location = /media/ only applies for exact
matches, not locations starting with /media/. Remove the =.

Django staticfiles_dirs not working correctly

I'm working with this website http://fotokluczniok.pl/ now.
If You press F12 You will see the staticfiles do not work correctly.
Here is my seetings.py code:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'/home/fotoklu/fotokluczniok/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
Here is urls.py code:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Project structure: CLICK HERE
How to solve this problem ?
I think you don't need to change something about the static files in the urls.py file. And also delete the variable STATIC_ROOT. In general your code in Settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join (BASE_DIR, 'static'),
]
I also had this problem. It helped me.
NOTE: Always restart the server after changes in settings.py

django The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

I'm deploying a Django application on heroku.
In my settings module, I have configured to host static files like
STATIC_ROOT = os.path.join(BASE_DIR, 'static_my_project')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_my_project')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
and urls.py
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But on deployment to heroku, it gives error as
SystemCheckError: System check identified some issues:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
may be help this.
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = ''
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
The problem is in the STATIC_ROOT and STATICFILES_DIRS there names cant be same.
static root is used for collectstatic command of django.
You can remove it if not using it.
And can also remove static files dirs if not changing its default position(inside django app) to somewhere else like base or something.