[英]404 error when getting file in static, django
I tried several attempts through googling, but it didn't work.我通过谷歌搜索尝试了几次尝试,但没有成功。 I don't think it was like this when I was working on another Jango project, but I don't know why.
当我在另一个 Jango 项目上工作时,我不认为它是这样的,但我不知道为什么。
urls.py网址.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.HomeView.as_view(), name = 'home'), #to home.html
path("blog/", include('blog.urls')),
]
# urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
settings.py设置.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIR = (os.path.join(BASE_DIR, 'static'),)
# STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
home.html主页.html
<img src="{% static 'assets/img/navbar-logo.svg' %}" alt="..." />
directory目录
Project-folder
|
|__config
| |__settings.py
| |__urls.py
|
|__app
|
|__template
| |__app
| | |__bla.html
| |__home.html
|
| #bootstrap
|__static
|__assets
|__img
...
If you've didn't loaded the static try this in your base.html or the home.html如果你还没有加载 static 试试这个在你的 base.html 或 home.html
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
An Excerpt from the docs .文档摘录。
Your project will probably also have static assets that aren't tied to a particular app.
您的项目可能还会包含不与特定应用程序绑定的 static 资产。 In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.
除了在应用程序中使用 static/ 目录之外,您还可以在设置文件中定义目录列表 (STATICFILES_DIRS),其中 Django 还将查找 static 文件。
So, use list of dictionaries and also it is STATICFILES_DIRS
not , you missed STATICFILES_DIR
S
.所以,使用字典列表,它也是
STATICFILES_DIRS
而不是 ,你错过了STATICFILES_DIR
S
。
Try this:尝试这个:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
Or this:或这个:
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Make sure that you have loaded the static tag.确保您已加载 static 标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.