简体   繁体   English

Django静态文件(找不到404)-Openshift

[英]Django static files (404 not found) - Openshift

I'm trying to run my very first project using Python/Django on Openshift and have problem with loading my static files. 我正在尝试在Openshift上使用Python / Django运行我的第一个项目,并且在加载静态文件时遇到问题。

I've read the https://docs.djangoproject.com/en/dev/howto/static-files/ multiple times I have been breaking my head over this for a full day but can't figure out the problem. 我已经多次阅读https://docs.djangoproject.com/zh-CN/dev/howto/static-files/ ,但我已经整整一整天都在为此烦恼,但无法弄清问题所在。

I'm running a developer server: 我正在运行开发人员服务器:

python manage.py runserver

setting.py 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')

template 模板

{% load static %}
<a href=""><img src="{% static "logo2.png" %}"></a>

urls.py 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()

The weirdest thing is that after pushing my app to openshift everything is working just fine but on localhost sth goes wrong. 最奇怪的是,在将我的应用程序推送到openshift之后,一切都工作正常,但是在localhost上却出错了。

Make long story short: 简而言之:

  • 127.0.0.1:8000/static/logo2.png - Not found 127.0.0.1:8000/static/logo2.png-找不到
  • mysite.rhcloud.com/static/logo.png - Yeah, I see the image mysite.rhcloud.com/static/logo.png-是的,我看到了图像

I hope it's clear and my problem is as stupid as I imagine. 我希望这很清楚,我的问题也像我想象的那样愚蠢。

Django 1.8, OS Windows Django 1.8,操作系统Windows

SOLUTION: 解:

I've deleted the 'django.contrib.staticfiles' from INSTALLED_APPS and add to the urls.py this peace of code: 我已经从INSTALLED_APPS中删除了'django.contrib.staticfiles',并添加了以下代码:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Are you in development mode? 您是否处于开发模式? If yes and if you don't have django.contrib.staticfiles in INSTALLED_APPS, you need to add this to urls.py: 如果是,并且在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)

Explained here . 在这里解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM