django无法加载静态文件

尝试了很多办法仍不见效,大佬们能帮看一下么,django小白~

配置文件是settings.py

import os

BASE_DIR = os.path.abspath(os.path.dirname(file))

DEBUG = True

STATIC_URL='/static/'

STATICFIELS_DIRS=( os.path.join(BASE_DIR,"static"), ) STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')

print("base dir: ",BASE_DIR) print("static dir:",STATICFIELS_DIRS)

加载的html文件是:

{% load static %} <img src="{% static 'img/myimg.png' %}">

python3.7 manage.py findstatic --verbosity 2 img/myimg.png base dir: /Users/xxx/Documents/Django-2.0.13/django/bin/autotest/autotest static dir: ('/Users/xxx/Documents/Django-2.0.13/django/bin/autotest/autotest/static',) System check identified some issues:<

<p><p>WARNINGS: No matching file found for 'img/myimg.png'.</p></p>

<p><p>Looking in the following locations: /usr/local/lib/python3.7/site-packages/django/contrib/admin/static /usr/local/lib/python3.7/site-packages/ckeditor/static /usr/local/lib/python3.7/site-packages/ckeditor_uploader/static /usr/local/lib/python3.7/site-packages/django_bootstrap_icons/static /usr/local/lib/python3.7/site-packages/pagedown/static</p></p>

感觉find的路径不对,但是setting配置和打印出来的路径跟预期是一样的。autotest是app的名称,png放在autotest/aotutest/static/img/myimg.png里面</p>

烦请大佬帮看一下~感谢!

评论 1

  • 已经解决: settings中BASE_DIR 改成

    BASE_DIR = Path(file).resolve().parent.parent

    对应的是:/Users/xxx/Documents/Django-2.0.13/django/bin/autotest

    应用的时候在app下新建static文件如,app名称为mytest对应的是 xxx/mytest/static/mytest/test.png

    xxx/mytest/static/mytest/css/base.css

    html文件中:

    <img src="{% static 'mytest/test.png' %}" alt="1">

    <link rel="stylesheet" href="{% static 'mytest/css/base.css' %}">