Django media file page not found - python

So, I'm trying to follow Django documentation about the static files and media files
I have a clean Django installation and I want to add the media folder. What I've done? I've changed the urls.py inside the project (not the app) and the settings.py as below
settings.py
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / "media"
STATICFILES_DIRS = [
BASE_DIR / "static",
]
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But I got the
Page not found (404) Request Method: GET Request
URL: http://127.0.0.1:8000/ Using the URLconf defined in
web_project.urls, Django tried these URL patterns, in this order:
admin/ ^media/(?P.*)$ The empty path didn’t match any of these.
I've also tried adding the 'django.template.context_processors.media' into TEMPLATES and using os as below
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
but nothing changes
What it could be?

Getting this error is the expected behavior because when you add a path to urlpatterns in urls.py in your example + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) you'll no more get access to http://127.0.0.1:8000/ (it just renders a “Congratulations!” page, with a rocket taking off. It worked! ) unless you specify a view to this empty path (which you didn't)
In your case, there is no URL leading to http://127.0.0.1:8000/ . the only paths you get access to are :
1- http://127.0.0.1:8000/admin/ for the admin page
2 - http://127.0.0.1:8000/media/(?P<path>.*)$ to access you media files
for example, if you have an image img.png in media folder you can access http://127.0.0.1:8000/media/img.png and view your image

The static function is only intended to serve static files during development and is not suitable for serving user-uploaded media files.
Try to make a condition check that whether DEBUG=True or not so:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Related

Accessing static file images through url not working django

I am currently trying to configure my django project so that I can access static file images via url. However, it is currentely saying that the image does no exist.
Here are my settings for static files:
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, '/static/images')
Here are my base project urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls'))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
And my folder structure is, in the root of my project: static->images->[all images in here]
Upon trying to access an image via url like this: http://127.0.0.1:8000/images/*proper image name*
It tells me that the image doesn't exist. Here is the call back:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/IMG_4622_2.JPG
Raised by: django.views.static.serve
'IMG_4622_2.JPG' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Does anyone know why this may be happening?
STATIC_URL specifies the url path to serve static files. In your case the setting points to /static/.
So that's where you have to access them - http://127.0.0.1:8000/static/images/*proper image name*

how to serve dynamic files in django?

I know there are many answer already, but unfortunately I failed to find a solution on them.
Basically I am trying to serve image files stored at server|/srv/post/users/folder/PIMAGES/ACTIVE/image.jpg to client, but with the configuration I have in my settings.py file, I am getting an http 404 file not found error.
my settings.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = '/srv/post/'
MEDIA_URL = '/post/'
and then if I refer to http://127.0.0.1:8000/post/users/folder/PIMAGES/ACTIVE/image.jpg
I just get 404 page not found.
#whocaresz you are referencing your MEDIA_ROOT and MEDIA_URL incorrectly, when you specified MEDIA_ROOT as srv/post/ then your image.jpg should be in post directory and to access it go to http://127.0.0.1:8000/post/image.jpg
for future students, I also had to add a static entry to my project urls.py file as below:
urlpatterns = [
url(r'^thisischanged/', admin.site.urls),
path('', include('someapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
or + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) at the end.

Django - Images aren't loading in second app

I got two apps 1. main, 2. contact inside my Django project. Both use the base.html file to get the head and the footer. Inside the footer is a image which perfectly work in the main app but inside the contact app it is not shown, there is no images displayed.
I tryed to change the settings but everything was set correct. I implement the static and media root
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
and inside the urls.py I do
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('contact/', include('contact.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If i open the inspector in firefox the path is the right one but the console throws a 404
GET http://localhost:8000/static/media/contact/contact_image.jpg 404
(Not Found)
How couldnt it work when the media folder is in the root of the app and the settings are the same in both apps?
can you try
http://localhost:8000/media/contact/contact_image.jpg
and also can you add urlpatterns this:
urlpatterns+=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Display Static file Django and rest-framework

I need to Display the static file, I follow Django document but it is not displayed.
1.In Models file
image = models.ImageField(upload_to='images/')
2.In urls.py file
from django.conf import settings
from django.conf.urls.static import static
...
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
3.In settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'lost_items/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
finally, I run this command
python manage.py collectstatic
when I GET API, the image in this URL
"image": "http://localhost:8000/media/images/n73.jpg"
So, When I click on it, I get the page not found, and:
The current path, media/images/money.jpg, didn't match any of these.
I assume that the images was uploaded by the user.
add this to your urls.py
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All uploaded files or "media" will have the Media URL. It only makes sense that Media URL must be added to urls.py

Django admin: image saved but error occured when click

I am ashamed to ask a question of that sort but I still can not solve my problem. I normally uploaded image in my media directory and can see image link in my admin but if I click on link I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/photo/img/9.jpg
Using the URLconf defined in TeamStudy.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, photo/img/9.jpg, didn't match any of these.
my project structure:
src
static/
photo/
img/
settings:
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'photo')
MEDIA_URL = '/photo/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'
urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
I suspect it perhaps quite simple and I miss something. Please point me.
You need to add the media MEDIA_ROOT and MEDIA_URL in your urlpatterns
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
check following links for more details
Accessing "Media" files in Django
Django classifies user files in two types
Static files
Media files
this link helps you understand the difference between them
Your issues deals with Media files.
In future development, you may need to serve static files, to serve them you will need to add
STATIC_ROOT and STATIC_URL to the urlpatterns in a similar way that MEDIA_ROOT and MEDIA_URL are added
Change your URL's patterns:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
for more info Django Docs
First point: don't put uploaded medias in you static directory - static is for static files that are part of your project (css, js etc) -, you want to use MEDIA_ROOT and MEDIA_URL for this.
wrt/ serving the static and media contents, it depends on the environment. On your local dev environment you want to use the staticfile.views to serve both static and medias as mentionned in the other answers, but don't do this in production : on a production server, you want to use your front web server to serve static (and media) contents.

Categories

Resources