簡體   English   中英

Django靜態文件(找不到404)-Openshift

[英]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上卻出錯了。

簡而言之:

  • 127.0.0.1:8000/static/logo2.png-找不到
  • mysite.rhcloud.com/static/logo.png-是的,我看到了圖像

我希望這很清楚,我的問題也像我想象的那樣愚蠢。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM