[英]Django static files (404 not found) - Openshift
我正在尝试在Openshift上使用Python / Django运行我的第一个项目,并且在加载静态文件时遇到问题。
我已经多次阅读https://docs.djangoproject.com/zh-CN/dev/howto/static-files/ ,但我已经整整一整天都在为此烦恼,但无法弄清问题所在。
我正在运行开发人员服务器:
python manage.py runserver
setting.py
STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'), 'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(WSGI_DIR, 'static')
模板
{% load static %}
<a href=""><img src="{% static "logo2.png" %}"></a>
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/index/')),
url(r'^index/', include('index.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
urlpatterns += staticfiles_urlpatterns()
最奇怪的是,在将我的应用程序推送到openshift之后,一切都工作正常,但是在localhost上却出错了。
简而言之:
我希望这很清楚,我的问题也像我想象的那样愚蠢。
Django 1.8,操作系统Windows
解:
我已经从INSTALLED_APPS中删除了'django.contrib.staticfiles',并添加了以下代码:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
您是否处于开发模式? 如果是,并且在INSTALLED_APPS中没有django.contrib.static文件,则需要将其添加到urls.py中:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在这里解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.