Django Media url returns 404 NOT FOUND

352
February 13, 2017, at 4:34 PM

I have a django project. In order to set up my media url, I followed the django doc Django doc - Managing static files:

For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:

from django.conf import settings from django.conf.urls.static import static

urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

my settings.py:

# ....
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_URL = 'http://127.0.0.1:8000'
# ...
MEDIA_URL_REL = '/media/'
MEDIA_URL = BASE_URL + MEDIA_URL_REL
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

my urls.py:

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
urlpatterns = [
 # ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

My uploaded files are found in media root, but when I access media url [ http://127.0.0.1:8000/media/proof/img.pdf]for it, it returns HTTP 404 NOT FOUND.

Answer 1

Please don't make any url with hard coded. This is more than bad practice.

BASE_URL = 'http://127.0.0.1:8000'

You can't write like that.

Your solution,

settings.py

MEDIA_URL = '/media/'

urls.py

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

Hope this works.

READ ALSO
Clustering intervals

Clustering intervals

Each of the rows of my dataframe is an interval represented by date1 and date2 and a user idFor each user id, I need to group together the intervals which are separated by a gap below a certain threshold

275
Can I use method as Column in Sqlalchemy?

Can I use method as Column in Sqlalchemy?

I wish when I save a User´╝î fullname will be called and the return value will be save to column fullname in db, and set_fullname will be called after the object is loaded from database

281
2 highest value of specific column and average value, in python

2 highest value of specific column and average value, in python

In csv file a have every hour in day (24 rows for every day), and 4 date, from January 19, to January 22So I need to find 2 hours period with highest Con Value for every day (Date) for every BI (BI Name)

264
I want to open a txt file in python while using unittest with selenium

I want to open a txt file in python while using unittest with selenium

When I try to open this , it gives me an error:

203