简体   繁体   中英

Django static page not found 404

Django css file not found error, loading css file failed, getting 404 file not found error also **i am getting another error:

Not allowed to load local resources.

Yes, I've tried c:/path/chrome.e xe - , all it does is launch a browser, doesn't navigate me to any url/website .

What you guys suggest, this is my code. I'll be posting only the code which I think is to be relevant. My Django version is the latest one,2.

Browser Chrome

GET http://127.0.0.1:8000/static/care/style.css 404 (Not Found)

settings.py

# Application definition

INSTALLED_APPS = [
    'care.apps.CareConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]


# SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

style.css is present inside the directory Care which is present inside another directory called static.

I tried with relative and 'absolute path', when I tried with absolute path, I am getting a different error

Not allowed to load local resources.

Just to be through, I actually copied the link from HTML source code and paste it in the browser and it does take me to the style.css file.Nothing is wrong with the path.

why Django not detecting css file ? I am facing the same issue in firefox browser too, except it doesn't list the error unlike Chrome

index.html

{% load staticfiles %}
  <link rel="stylesheet" type="text/css"   href="{% static  'care/style.css' %}" />

If your MEDIA_URL is defined as /media/ , you can do this by adding the following snippet to your APP urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  1. Set the STATIC_ROOT setting to the directory from which you'd like to serve these files, for example:

    STATIC_ROOT = "/var/www/example.com/static/"

Run the collectstatic management command:

$ python manage.py collectstatic

This will copy all files from your static folders into the STATIC_ROOT directory.

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