1

I have problem withe icons in admin panel in my Django app. I have all files in my root media folder:

'/files/dev/projects/mynewproject/media'

but in console I get 404 error.

My urls.py

urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + patterns('',
    # ...
)

My MEDIA_ROOT:

MEDIA_ROOT = '/files/dev/projects/mynewproject/media'

Response in terminal:

/media/adminextra/js/jquery.js HTTP/1.1" 404 1829
git-e
  • 269
  • 1
  • 4
  • 15
  • Are you sure that jquery should reside under MEDIA_ROOT instead of STATIC_ROOT? MEDIA is meant for resources uploaded by the user(s) while STATIC is for app resources like JS/CSS etc. – Risadinha Nov 14 '16 at 11:12
  • Yes, I'm sure because 'adminextra' folder is for files that I use in admin panel – git-e Nov 14 '16 at 11:17
  • Where is the `jquery.js` located on your disk? What is the value of `settings.DEBUG`? – Alasdair Nov 14 '16 at 11:53
  • File jquery is in: /files/dev/projects/mynewproject/project/concerthall/media/adminextra/js settings.DEBUG = True – git-e Nov 14 '16 at 12:17
  • Assuming your `MEDIA_URL` is `/media/` it will create a `media` folder in the root of the local storage at user's end, and it will search for files there. Same is the case for `STATIC_URL`. So change your MEDIA_URL to `/files/dev/projects/mynewproject/media`. Even i had a hard time figuring the solution for this same exact problem. Another problem is that. I recommend checking the path of MEDIA_ROOT and BASE_DIR which is returned by DEBUG log. – hulkinBrain Nov 14 '16 at 12:50

1 Answers1

0

If

MEDIA_ROOT = '/files/dev/projects/mynewproject/media'

and the URL is

http://localhost:8000/media/adminextra/js/jquery.js

then the local path to the file must be:

/files/dev/projects/mynewproject/media/adminextra/js/jquery.js

whereas you say (in the comment) that your file is located at:

/files/dev/projects/mynewproject/project/concerthall/media/a‌​dminextra/js

Of course, it's not found.

This works differently for STATIC paths and URLs because you have the collectstatic and the STATIC files finder mechanism in between. I recommend placing your resources into the STATIC roots because that is where they belong. MEDIA is not meant for this.

Also, best practice should avoid hard coded absolute file paths in your settings. This won't work in a shared project.

Example setup in <git_root>/mynewproject/settings/base.py:

BASE_APP_DIR = os.path.dirname(os.path.realpath(project_module.__file__))
PROJECT_DIR = os.path.dirname(BASE_APP_DIR)

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_collected')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)
STATICFILES_DIRS = (
    os.path.join(BASE_APP_DIR, 'static'),
)

with your web resources (JS/CSS/img) residing under <git_root>/mynewproject/static and lower app directories and being output by collectstatic into <git_root>/static_collected. On production you would have NGINX or Apache deliver the files in that latter directory.

Place your file into:

mynewproject/project/concerthall/static/a‌​dminextra/js/jquery.js

(I take it, concerthall is an app with a models.py file.)

In your admin class that uses it:

class Media:
    js = ("a‌​dminextra/js/jquery.js",)
Risadinha
  • 16,058
  • 2
  • 88
  • 91