I am having some problems in figuring out the media path problem in django. In the settings开发者_Go百科.py file, I have ADMIN_MEDIA_PREFIX = '/media/admin/'. So certain css and js files like forms.css,RelatedObjectLookup.js files are having the path like this
/media/admin/js/admin/RelatedObjectLookup.js and /media/admin/css/forms.css.
There is not folder in my project like /media/admin. However when I run the server using manage.py script and browse the page, the above css and js files are being loaded. I can see that the files forms.css and RelatedObjectLookup.js are in the django itself. However, I am confused about the path in the django itself. It is something like this
/usr/local/lib/python2.7/site-packages/django/contrib/admin/media/js/calendar.js
So, I am confused, how are the files being fetched. In the server log I can see the GET request for other files but not for these files(forms.css and RelatedObjectLookup.js)
I have the following in myserver request log after running manage.py.
[23/Aug/2011 02:20:36] "GET /media/js/DateTimeShortcuts.js HTTP/1.1" 304 0
[23/Aug/2011 02:20:36] "GET /media/js/custom/new_appointment_new.js HTTP/1.1" 304 0
[23/Aug/2011 02:20:36] "GET /media/js/fancybox/jquery.mousewheel-3.0.4.pack.js HTTP/1.1" 304 0
[23/Aug/2011 02:20:36] "GET /media/js/fancybox/jquery.fancybox-1.3.4.pack.js HTTP/1.1" 304 0
[23/Aug/2011 02:20:36] "GET /media/js/fancybox/jquery.fancybox-1.3.4.css HTTP/1.1" 304 0
As you can there is no request for the forms.css and RelatedObjectLookup.js files.
Any suggestions?
Have a look at the AdminMediaHandler that is used by the runserver command. As you can see, the AdminMediaHandler doesn't log anything.
The ADMIN_MEDIA_PREFIX
is an URL path. If you are running the server with runserver
, Django's server takes care of resolving requests to http://localhost/media/admin/whatever.js
and serving up the admin's static files. It doesn't print GET requests for those files to the console.
Howewver, if you're not running with runserver
, then you have to make sure that the path that ADMIN_MEDIA_PREFIX
specifies does actually serve up the static files for the admin. For instance, in one of our sites, our STATIC_URL
is http://static.oursite.com/
and our STATIC_ROOT
is /srv/django-static/oursite/
. The ADMIN_MEDIA_PREFIX
is set to http://static.oursite.com/admin/
. When we run python manage.py collecstatic
, it takes all the static files and puts them in the STATIC_ROOT
. It also makes a folder called admin
in the STATIC_ROOT
and puts all the admin's files there. A different server is responsible for serving stuff on http://static.oursite.com/
and it simply serves up the STATIC_ROOT
folder, which also has the admin
folder in it. That way, the normal static files get resolved, as well as those for the admin.
精彩评论