1

静的ファイルを読み込もうとしていますが、次のエラーが表示されます:

GET http://127.0.0.1:8000/static/css/user/style.css net::ERR_ABORTED 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/css/user/style.css 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:149
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:1

エラー画像を表示するには、ここをクリックしてください。

私のコード:

-管理者

設定.py

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

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Welcome.urls')),
    path('auth/', include('Authentication.urls')),
    path('ad/', include('Ads.urls')),
    path('user/', include('UserDashboard.urls')),
    path('admin/', include('AdminDashboard.urls')),
]

if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  • アプリ

テンプレート

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

ディレクトリ構造

ここをクリックして、プロジェクト ディレクトリ構造のイメージを表示します

コードの説明

  • 単純にルート ディレクトリに静的ファイルを追加し、それらをテンプレートにインポートしようとしましたが、css ファイルが読み込まれません
  • ただし、一部のメディア ファイルは、このメディアのように正常に読み込まれます<link rel="shortcut icon" type="image/jpg" href="{% static 'img/logo.png' %}" />
  • また、ディレクトリ構造では、静的フォルダーの同じ場所に img フォルダーと css フォルダーが表示され、image フォルダーの画像は読み込まれますが、css フォルダーの css は読み込まれません。
4

2 に答える 2

0

Settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "static_collect")

urls.py

+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns の括弧を閉じた後にこれらを追加します。

このコマンドを起動します python manage.py collectstatic

このすべてのセットアップの前に、プロジェクト フォルダー内に静的フォルダーを作成します。これがうまくいくことを願っています

于 2021-09-18T11:48:15.673 に答える