简体   繁体   English

Django; 无法加载静态文件

[英]Django; cannot load static files

I've been working on Django and my static files cannot loaded. 我一直在使用Django,但是我的静态文件无法加载。 My project structure is like this 我的项目结构是这样的

blog/
|___apps/
|     |__static/
|     |     |__css/
|     |     |__js/
|     |
|     |__templates/
|     |     |__base.html
|     |     |__feeds/
|     |
|     |__feeds/
|
|___blog/
       settings.py

my settings.py is like this 我的settings.py就是这样

APPS_DIR = os.path.join(BASE_DIR, 'apps')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(APPS_DIR, 'static'),
   )

The weird thing is only for index.html , static files can be loaded even though html files are inside templates/feeds/ . 奇怪的是仅适用于index.html ,即使html文件位于templates/feeds/ ,也可以加载静态文件。

there might be fault in my urls.py because the error says like 我的urls.py可能存在错误,因为错误显示为

Not Found: /feeds/static/css/styles.css 找不到:/feeds/static/css/styles.css

seems like Django tries to find static files inside static/feeds/ . 似乎Django试图在static/feeds/找到静态文件。

urls.py is like this urls.py就是这样

urlpatterns = [
path('', views.index, name='index'),

path('feeds/', include([
    path('list', views.list_entry, name='list_entry'),

I don't want to create app name folder inside static if possible. 如果可能的话,我不想在static创建应用程序名称文件夹。 How can I fix this? 我怎样才能解决这个问题?

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

Try importing these in the project url and then add the following : 尝试将这些导入项目URL中,然后添加以下内容:

urlpatterns = [
  path('', views.index, name='index')
  ....
  ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

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

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