【发布时间】:2020-07-06 15:05:14
【问题描述】:
我试图搜索很多关于这个问题的帖子,任何一个都不能解决我的问题,很遗憾,我在某些方面无法理解。这是帖子。
Django cannot find static files. Need a second pair of eyes, I'm going crazy
我想这是与我最相似的情况,有人对此给出了非常好的答案,但它无法解决我的问题:/
这是我的文件集
- beerlog
- beerlog
- settings.py
- ...
- posting
- urls.py
- templates
- posting
- base.html
- index.html
- post.html
- posting.html
- static
- posting
- style.css
- ...
- static
- registration
- ...
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
/posting/templates/posting/base.html
<!DOCTYPE html>
{% load static %}
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Beerlog</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/posting/sytle.css">
{% block extra_head %}{% endblock %}
</head>
<body>
<h2 class='headline'>Welcome to beerlog!</h2>
<ul class="sidenav">
{% if user.is_authenticated %}
<li>Welcome back {{ user.get_username }}!</li>
<li><a href="{% url 'logout' %}?next={{request.path}}">Logout</a></li>
{% else %}
<li><a href="{% url 'login' %}?next={{request.path}}">Login</a></li>
{% endif %}
</ul>
{% block content %}
{% endblock %}
/posting/urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
app_name = 'posting'
urlpatterns = [
path('', views.index, name='index'),
path('<int:post_id>', views.post, name='post'),
path('posting', views.posting, name='postingform'),
path('base', views.base, name='base')
]
/beerlog/urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.urls import static, staticfiles_urlpatterns
from . import settings
urlpatterns = [
path('posting/', include('posting.urls')),
path('admin/', admin.site.urls),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
path('accounts/', include('django.contrib.auth.urls')),
]
我设置的静态文件和django官方文档一样的模板,模板工作得很好但是为什么css不起作用?请帮帮我:(绝望
【问题讨论】:
-
尝试在你的设置中添加这个 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static") ]
-
兄弟你是通过静态函数导入css文件的吗?
<link rel="stylesheet" href="/posting/sytle.css">??还是只是链接??对 settings.py 和{% static 'path_to_css' %}使用上面的注释使用它代替href="/posting/sytle.css"并且你的 css 名称也是错误的 -
@NoahLc 我尝试这样做,但他们说我不能在 STATICFILES_DIRS 中包含 STATIC_ROOT。
-
您在文件名
sytle.css而不是style.css中有错字。 -
那你为什么不点赞
标签: django django-templates django-staticfiles