如何使静态样式表在 django 中工作?

2024-01-02

通过正常运行此代码html它运行起来也很有风格。但每当我在 django 中运行它时,它都不会运行,只显示文本。

views.py

def index(request):
    return render(request, 'index.html');

main url

urlpatterns = [
    path('', include('app1.urls')),
    path('admin/', admin.site.urls)
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

索引.html

<!-- Animate.css -->
<link rel="stylesheet" href="{% static '../static/css/animate.css' %}">
<!-- Icomoon Icon Fonts-->
<link rel="stylesheet" href="{% static '../static/css/icomoon.css' %}">
<!-- Simple Line Icons -->
<link rel="stylesheet" href="{% static '../static/css/simple-line-icons.css' %}">

<section id="fh5co-home" data-section="home" style="background-image: url(../static/images/W1.png);" data-stellar-background-ratio="0.5">
    <div class="gradient"></div>
    <div class="container">
        <div class="text-wrap">
            <div class="text-inner">
                <div class="row">
                    <div class="col-md-8 col-md-offset-2 text-center">
                     <h1 class="to-animate">Welcome in the World of White Hats</h1>
                        <h2 class="to-animate">Your Problem with auto Solution</h2>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

文件夹结构


static https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-static模板标签添加STATIC_ROOT到给定的 URL。所以检查一下静态根 https://docs.djangoproject.com/en/3.0/howto/static-files/#configuring-static-filessettings.py 中的值并更改相对路径

{% static '../static/css/animate.css' %}

to

{% static 'css/animate.css' %}

请注意,您需要在模板的开头添加此内容才能使static work:

{% load static %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使静态样式表在 django 中工作? 的相关文章

随机推荐