首页 > 解决方案 > 每当我尝试加载静态文件时,它都会给出错误 404 not found

问题描述

每当我尝试加载静态文件时,它都会给出错误 404 not found。但是,当检查它在哪个目录中显示时,它会显示 /account/login/static/accounts/style.css,而 account/login 甚至不是目录。错误消息未找到:/account/login/static/accounts/style.css

我到处搜索,找不到如何更改目录。

这是 settings.py 文件:

STATIC_URL = '/static/'

LOGIN_REDIRECT_URL = '/accounts'

STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
   ]

这是我的帐户/urls.py:

from django.contrib.auth.views import LoginView

from django.urls import path
from accounts import views
app_name = "users"
urlpatterns = [
    path('', views.home, name='home'),
    path('login/', LoginView.as_view(template_name='accounts/login.html'), name="login")
    ]

这是我的教程/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('account/', include('accounts.urls'))
]

这是我的view.py:

from django.shortcuts import render, HttpResponse

def home(request):
    return render(r[enter image description here][1]equest, "accounts/login.html")[enter image description here][1]

这是我的目录/文件的图像:https ://i.stack.imgur.com/vdk0X.png

标签: pythondjangodjango-staticfiles

解决方案


运行命令:

python manage.py runserver

然后从您的模板地址中清除该帐户前缀。
将此更改为此accounts/login.htmllogin.html 您应该在模板文件 login.html 中进行更多更改以加载静态文件。我建议您先阅读文档


推荐阅读