未找到Django静态页面404

时间:2022-10-10 10:01:18

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

Django css文件未找到错误,加载css文件失败,得到404文件未找到错误也**我收到另一个错误:

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.

是的,我已经尝试了c:/path/chrome.e xe - 它所做的就是启动浏览器,不会导航到任何网址/网站。

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.

你们建议的是,这是我的代码。我将仅发布我认为相关的代码。我的Django版本是最新版本,2。

Browser Chrome

浏览器Chrome

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

settings.py

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.

style.css存在于Care目录中,该目录位于另一个名为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.

为了完成,我实际上从HTML源代码中复制了链接并将其粘贴到浏览器中,它确实将我带到了style.css文件中。路径没有任何问题。

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

为什么Django没有检测到css文件?我在firefox浏览器中也遇到了同样的问题,除了它没有列出与Chrome不同的错误

index.html

的index.html

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

1 个解决方案

#1


0  

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

如果您的MEDIA_URL定义为/ media /,您可以通过将以下代码段添加到您的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设置设置为您要为其提供这些文件的目录,例如:

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

    STATIC_ROOT =“/ var / www / example / static /”

Run the collectstatic management command:

运行collectstatic管理命令:

$ python manage.py collectstatic

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

这会将静态文件夹中的所有文件复制到STATIC_ROOT目录中。

#1


0  

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

如果您的MEDIA_URL定义为/ media /,您可以通过将以下代码段添加到您的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设置设置为您要为其提供这些文件的目录,例如:

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

    STATIC_ROOT =“/ var / www / example / static /”

Run the collectstatic management command:

运行collectstatic管理命令:

$ python manage.py collectstatic

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

这会将静态文件夹中的所有文件复制到STATIC_ROOT目录中。