0

需要按照建议从公共目录访问静态文件

https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/

命令,python manage.py collectstatic

复制到 STATIC_ROOT 但在 html 中引用它时,它会抛出错误

Not Found: /static/teststatic/images/product-5.jpg

如何使它与来自 STATIC_ROOT 位置的文件一起工作?

我已经尝试过了并且已经工作了

<img src="{% static 'teststatic/static/teststatic/images/product-5.jpg' %}" alt="Trulli" width="500" height="333">

网址.py

from django.urls import path
from . import views

app_name = 'teststatic'

urlpatterns = [
    path('tstatic/', views.tstatic, name='tstatic'),
]

视图.py

from django.shortcuts import render

# Create your views here.


def tstatic(request):
    return render(request, 'tstatic.html', {})

设置.py

DEBUG = True
.
.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = (
    ('testcss', os.path.join(BASE_DIR, 'testcss', 'static', 'testcss')),
    ('helpdesk', os.path.join(BASE_DIR, 'helpdesk', 'static', 'helpdesk')),
    ('teststatic', os.path.join(BASE_DIR, 'teststatic', 'static', 'teststatic')),
    (os.path.join(BASE_DIR, 'staticfiles'))
)

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

tstatic.html

{% load staticfiles %}
<!DOCTYPE html>
<html>
<body>

<h2>HTML Image</h2>
<img src="{% static 'teststatic/images/product-5.jpg' %}" alt="Trulli" width="500" height="333">

</body>
</html>

错误 :

2021-04-27 12:42:47,744: Not Found: /static/teststatic/images/product-5.jpg
4

0 回答 0