Django static files not loading with default setup

情到浓时终转凉″ 提交于 2019-12-24 03:16:24

问题


So I installed Bitnami Django stack, and enabled the admin module, and followed the tutorial for creating an admin menu for "Polls".

However, when I go to /admin/ everything is white plaintext. All the css and images are 404 error.

All I did was:

enable in settings.py installed_apps:

'django.contrib.admin',

In urls.py UNcommented:

from django.contrib import admin
admin.autodiscover()
url(r'^admin/', include(admin.site.urls)),

uncommented.

In settings.py, I tried using default settings and also tried this:

MEDIA_ROOT = ''
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
  os.path.join(SITE_ROOT, 'static/'),
)

Nothing seems to work, it refuses to find static files in /admin/media/css/ etc.

I made sure my windows PATH has /bin of django. I even tried including /contrib, nothing helps.

I have installed Django to: C:\DjangoStack\apps\django

I have installed my project to: C:\Users\dexter\BitNami DjangoStack projects\Alpha and I type: localhost/Alpha/admin to go to admin.


回答1:


I almost missed the answer to this until I re-read your question and finally caught the bit in the last line: "and I type: localhost/Alpha/admin to go to admin". That means all your URL settings are wrong.

Currently, you have:

MEDIA_URL = '/media/'
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

Whereas, these should be:

MEDIA_URL = '/Alpha/media/'
STATIC_URL = '/Alpha/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

Additionally, you don't need "static/" in STATICFILES_DIRS. So remove that setting.




回答2:


I'm from the BitNami Team and I just saw this issue. I'm not sure if you are using and older version but at least in the newer version of the BitNami DjangoStack you just need to make sure that the ADMIN_MEDIA_PREFIX point to /static/admin/ if you are following the instructions in https://docs.djangoproject.com/en/1.3/intro/tutorial02/ . You don't need to copy the static files in your project directory because django automatically will use the files in django/contrib directory.

However currently we are setting the ADMIN_MEDIA_PREFIX to /static/admin/media because the behavior is different when the application is served by Apache instead of the django server. We realize that this may be a bit confusing for users that are just starting with django and we are looking at this on our side to keep the default configuration for the new projects but also allow the demo project to be served by Apache.



来源:https://stackoverflow.com/questions/7666016/django-static-files-not-loading-with-default-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!