簡體   English   中英

使用 django 加載靜態 css 文件的問題

[英]Problem loading a static css file with django

我正在嘗試加載靜態 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類更改以下內容。

樣式文件
不管你給你的班級起什么名字。 無需將它們命名為與您的表單名稱相同的名稱。

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

現在在您的home.html中將該類添加到您的表單中。 (尋找評論)。 所以現在這個容器內的所有元素也都有 size 屬性。

{% 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 %}

編輯:看起來您正在使用引導程序,但似乎沒有加載它。 如果您還沒有在base.html添加引導程序,請添加它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM