简体   繁体   English

通过Django提供静态文件时找不到页面

[英]page not found when serving static files via django

so in settings.py I have 所以在settings.py中

STATIC_URL = '/static/'

and also 并且

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

hence as you can see django.contrib.staticfiles is there and moreover Debug = True 因此,如您所见,django.contrib.staticfiles存在,而且Debug = True

and in my app directory I have a file existing in this path 在我的应用程序目录中,此路径中存在一个文件

myapp/static/css/myapp.css

However, when I go to http://localhost/myapp/static/css/myapp.css 但是,当我转到http://localhost/myapp/static/css/myapp.css时

it ends up returning 它最终返回

Page not found (404)
Request Method:     GET

what am I doing wrong and how can I get django to serve static files properly? 我在做什么错,如何让django正确提供静态文件?

Include this in your project/urls.py 将此包含在您的project / urls.py中

from django.conf import settings

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

Use the option STATICFILES_DIRS in your settings. 在您的设置中使用选项STATICFILES_DIRS

An example: 一个例子:

# settings.py
...
STATICFILES_DIRS = [
    "/path/to/your/static/files/folder",
]
...

In your templates, use the static template tag like 在模板中,使用静态模板标记,例如

# some_page.html
...
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>
...

or hard-code like 或类似的硬编码

# some_page.html
...
<img src="/static/my_app/example.jpg" alt="My image"/>
...

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

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