所以我一直在尝试在 Django 中提供模板文件。我的主要问题是找到了
index.html
页面,但找不到静态文件(js、css、png)文件。它们都与 index.html
文件位于同一目录下。这是我的配置:
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
},
]
urls.py(主要)
path('rest-framework/', include('rest_framework.urls')),
path('api/', include('accounts.urls')),
path('admin/', admin.site.urls),
url(r'^rest-auth/', views.obtain_auth_token),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urls.py(应用程序)
urlpatterns = [
path('homepage/', views.index, name='index'),
]
views.py
def index(request):
return render(request, 'index.html')
这是我得到的确切错误:
[15/May/2024 08:31:40] "GET /api/homepage/ HTTP/1.1" 200 1837
Not Found: /api/homepage/flutter.js
[15/May/2024 08:31:40] "GET /api/homepage/flutter.js HTTP/1.1" 404 2674
Not Found: /api/homepage/manifest.json
[15/May/2024 08:31:40] "GET /api/homepage/manifest.json HTTP/1.1" 404 2683
我的模板文件目录是这样的:
accounts\
-migrations\
-templates\
server\
media\
static\
manage.py
requirements.txt
index.html
<base href="/">
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<title>bilmarkgorevdagilimi</title>
<link rel="manifest" href="manifest.json">
<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = '2027642995';
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
如何解决这个问题?
更新(对于anupsabraham):
PS C:\Users\....\server> python manage.py findstatic --verbosity 2 flutter.js
No matching file found for 'flutter.js'.
Looking in the following locations:
C:\Users\...\AppData\Local\Programs\Python\Python38\lib\site-packages\django\contrib\admin\static
C:\Users\...\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\static
{% load static %}
<script src="{% static 'flutter.js' %}" defer></script>
static
模板标签,Django 才能搜索静态文件。 链接{% load static %}
<base href="/">
<!-- Favicon -->
<link rel="icon" type="image/png" href="{% static 'favicon.png' %}"/>
<title>bilmarkgorevdagilimi</title>
<link rel="manifest" href="{% static 'manifest.json' %}">
<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = '2027642995';
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="{% static flutter.js %}" defer></script>
STATICFILES_DIRS
中定义 settings.py
。 链接