【问题标题】:django cannot get static css to work in template base.htmldjango 无法让静态 css 在模板 base.html 中工作
【发布时间】:2020-07-20 10:08:12
【问题描述】:

我正在尝试设置站点范围的 css 文件。

如果我将 css 直接放在我的块内容中,我的 css 将呈现

cat register/templates/register/register.html        
{% extends "base.html" %}

{% block title %}Create an Account{% endblock %}


{% load crispy_forms_tags %}

{% block content %}

<style>
.registration_base {
        padding-top: 50px;
        margin: auto;
        width: 30%;
}
</style>

<div class="registration_base">

        <form method="POST" class="form-group">
                {% csrf_token %}
                {{ form|crispy }}
                <button type="submit" class="btn btn-success">Register</button>
        </form>
</div>
{% endblock %}

它会正常运行,但我希望在我的 base.html 文件中从站点范围内的 style.css 文件调用代码。

我无法让 css 文件实际呈现。

在我的

templates/base.html

我有 更新:

$ cat templates/base.html        
<!doctype html>
<html>
<head>
        <title>Universal Trader</title>
        {% load static %}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare
.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

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


</head>
<body>

        {% include 'navbar.html' %}
        {% block content %}
                replace me
        {% endblock %}

</body>
</html>

在我的

register/static/style.css

我有相同的css代码:

.registration_base {
        padding-top: 50px;
        margin: auto;
        width: 30%;
}

我可以跑

#python3 manage.py findstatic style.css
Found 'style.css' here:
  /home/user/www/src/register/static/style.css

它找到了,但我的 css 格式永远不会改变。

我已经安装了应用程序

site/settings.py


INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'pages',
        'register.apps.RegisterConfig',
        'crispy_forms',
]

更新了 settings.py

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

更新了 nginx/sites-enabled/站点静态信息。

server{
        location /static {
                alias /home/user/www/src/exchange/static; # your Django project's static files - amend as required
        }
    }

如何正确包含站点范围的 css 文件?

更新:

#cat register/forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User    

class RegisterForm(UserCreationForm):
        email = forms.EmailField()

        class Meta:
                model = User
                fields = ["username", "password1", "password2", "email"]

#cat register/views.py        
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from .forms import RegisterForm

# Create your views here.
def register(response):
        if response.method == "POST":
                form = RegisterForm(response.POST)
                if form.is_valid():
                        form.save()

                return redirect("/")
        else:
                form = RegisterForm()

        form = RegisterForm()
        return render(response, "register/register.html",{"form":form})

更新:

我可以浏览网址

http://192.168.42.13:8080/static/style.css

很好,但我的 base.html 显然不能

我重新启动了服务器。同样的问题。

再次更新:

甚至

<link rel="stylesheet" type="text/css" href={% static "http://192.168.42.13/static/style.css" %}>

base.html 失败

【问题讨论】:

  • Nginx 找到这个文件了吗?您对此 CSS URL 请求的响应状态是什么?
  • @IvanStarostin 我无法从任何 url 访问/找到 style.css 文件。就好像它拒绝公开一样。
  • 那么您会得到哪种 HTTP 状态作为响应? 404? 403? 500?
  • @IvanStarostin 我刚刚更新了。哇,我可以浏览到192.168.42.13:8080/static/style.css 只是找到但我的 base.html 无法读取相同的公共文件。我一定是在哪里搞砸了。
  • 所以 style.css 是可以访问的,URL 很好,但是它不适用于你的页面,对吧? 1) 修复您的样式表定义 - 将它们全部移至 head 2) 将您自己的 css 放在此列表的最后。

标签: css django django-templates


【解决方案1】:

我建议使用“静态”标签,请参阅https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#static。您的 base.html 中的链接将是:

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

不要忘记在模板顶部加载标签:

{% load static %}

您可以在设置文件的 STATICFILES_DIRS 参数中指定 Django 在哪里查找静态文件,如下所示:

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

有关如何设置的更多信息,请参阅https://docs.djangoproject.com/en/3.0/ref/settings/#staticfiles-dirs

【讨论】:

  • 这些更改没有任何效果。 :(
  • 从您的 findstatic 结果 /register/static/style.css 来看,您的 style.css 似乎在 BASE_DIR 中(我错过了)。在这种情况下,请尝试 STATICFILES_DIRS 中的 os.path.join(BASE_DIR, 'static'),而不是我上面建议的。
  • 我尝试了 STATICFILES_DIRS = [ os.path.join(BASE_DIR, '/static/'), ],然后是 os.path.join(BASE_DIR, '/static'),然后是 os.path。加入(BASE_DIR,“静态”)。同样的问题。
  • 我看到您已将您的 href 更新为 {% static "/static/style.css" %}。我理解这个工作的方式是 Django 在 STATICFILES_DIRS 中查找路径,将描述的子目录“静态”附加到 BASE_DIR,然后查找文件名。所以当文件是/home/user/www/src/register/static/style.css,那么你的BASE_DIR是/home/user/www/src/register/,设置应该是STATICFILES_DIRS = [os.path.join (BASE_DIR, 'static/') (不确定是否需要/,html代码应该是“{% static 'style.css' %}”。别忘了外面是双引号,里面是单引号。
  • 衷心感谢!
猜你喜欢
  • 2015-10-01
  • 2022-06-14
  • 2012-02-29
  • 2020-10-27
  • 1970-01-01
  • 2015-01-02
  • 2021-03-21
  • 2018-08-03
  • 2016-12-13
相关资源
最近更新 更多