简体   繁体   中英

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.

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.

I'm running a developer server:

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')

template

{% 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()

The weirdest thing is that after pushing my app to openshift everything is working just fine but on localhost sth goes wrong.

Make long story short:

  • 127.0.0.1:8000/static/logo2.png - Not found
  • mysite.rhcloud.com/static/logo.png - Yeah, I see the image

I hope it's clear and my problem is as stupid as I imagine.

Django 1.8, OS Windows

SOLUTION:

I've deleted the 'django.contrib.staticfiles' from INSTALLED_APPS and add to the urls.py this peace of code:

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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