问题描述
我尝试搜索有关此问题的许多帖子,但不幸的是,任何一篇无法解决我的问题并且在某些方面我都无法理解的帖子。这就是帖子。
我想这与我最相似,有人对此给出了很好的答案,但是不能't解决我的问题:/
I tried to search many posts about this problem and any one those couldn't solve my problem sadly and I couldn't understand on some points.This is the post.
Django cannot find static files. Need a second pair of eyes, I'm going crazy
I guess this is the most similar case with me and someone gave very nice answer for it, but it couldn't solve my problem :/
这是我的文件集
This is my file set
- 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
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
/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
/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
/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不起作用?请帮助我:(绝望
I set static file same as django official documents like templates, templates work well but why css doeesn't work? Please help me guys :( Desperate
推荐答案
将发布
文件夹添加到STATICFILES_DIRS作为从STATIC_ROOT文件夹的附加文件夹(用于收集静态文件):
Add posting
folder into STATICFILES_DIRS as an additional (to the STATIC_ROOT) folder for collecting static files from:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'posting/static'),
]
在DEBUG = true中,可以访问这些文件。在移至DEBUG = false之前,运行 collectstatic
和发布/静态
中的文件和文件夹将被复制到STATIC_ROOT中(您可以立即尝试)。
In DEBUG=true it is enough to reach to those files. Before moving to DEBUG=false run collectstatic
and files and folders from posting/static
will be copied into STATIC_ROOT (you can try this right now).
然后从带有 static
标签的模板中引用您的静态文件:
Then refer your static files from template with static
tag:
{% import static %}
<link rel="stylesheet" href="{% static '/posting/style.css' %}">
如果仍然无法正常工作-从呈现的模板中获取URL,然后尝试手动打开它,然后检查响应状态。
If still not working - grab the URL from rendered template and try to open it manually, then check response status.
还要确保您已添加使Django在DEBUG = true中提供静态文件。请注意,Django不会在DEBUG = false中提供静态文件。
Also make sure you've added url patterns mentioned here to make Django serve static files in DEBUG=true. Note, Django won't serve static files in DEBUG=false.
这篇关于Django不加载css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!