gpt4 book ai didi

python - 使用 django 加载静态 css 文件时出现问题

转载 作者:行者123 更新时间:2023-11-28 14:40:19 25 4
gpt4 key购买 nike

我正在尝试加载一个静态 css 文件,但没有成功:

主页.html:

{% extends 'base.html' %}

{% block body%}
{% load static %}
<link rel="stylesheet" href="{% static 'home/style.css' %}" type="text/css">
<div class="container">
<br/>
<form method="post">
{% csrf_token %}
{{ form.post }}
<br />
<button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{ post.post }}</h1>
<p>Posted </b> on {{ post.created }}</p>
{% endfor %}
</div>
{% endblock %}

样式.css:

.HomeForm {
size:20;
}

表单.py:

class HomeForm(forms.ModelForm):
post = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'How are you feeling?',
'size': '20',

}
))

我感觉我在错误的地方加载了静态文件,有什么解决办法?提前致谢。

最佳答案

您正在正确加载 css 文件。但这不是将 css 类应用于 django 表单的方式。

首先,您已经在 forms.py 中为您的字段提供引导属性。现在应用 css 类更改以下内容。

style.css
不管你给你的类(class)起什么名字。无需将它们命名为与您的表单名称相同。

.home-post { 
size:30; // Increase this to see difference
}

现在在您的 home.html 中将类添加到表单中。 (寻找评论)。所以现在这个容器中的所有元素也都具有大小属性​​。

{% extends 'base.html' %} {% block body%} {% load static %}
<link rel="stylesheet" href="{% static 'home/style.css' %}" type="text/css">
<div class="container home-post"> <!--This is how you need to apply-->
<br/>
<form method="post">
{% csrf_token %} {{ form.post }}
<br />
<button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2> {% for post in posts %}
<h1>{{ post.post }}</h1>
<p>Posted on {{ post.created }}</p>
{% endfor %}
</div>
{% endblock %}

编辑:看起来你正在使用 Bootstrap 但似乎没有加载它。如果您尚未在 base.html

中添加 Bootstrap ,请添加它

关于python - 使用 django 加载静态 css 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53047496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com