簡體   English   中英

Django 在靜態文件中使用靜態文件 URL

[英]Django Use Static files URL in Static Files

我想在我的style.css文件中使用自定義字體,該字體將由StaticFiles

我知道我可以用這樣的東西對其進行硬編碼:

@font-face {
  font-family:"NotoSansArabic";
  font-style:normal;
  src:
  url("/static/myApp/fonts/NotoSansArabicUI-ExtraBold.ttf") format("truetype")
}

我正在尋找類似的東西:

{% static 'myApp/fonts/NotoSansArabicUI-ExtraBold.ttf' %}

有沒有其他方法可以做到這一點?

font 和 style.css 都用作Static

要使{% static %}工作,您需要有一個 Django 模板; 不是靜態文件。 這實際上是可能的:您可以創建一個名為 ie djangostyle.css的文件並將其放在您的templates文件夾中。 在此文件中,您可以照常使用{% static %} 然后,從您的模板中,您將使用{% include %}引用此文件。

如果您想查看完整示例,請查看我的 Django PDF 指南文章: https : //spapas.github.io/2015/11/27/pdf-in-django/#changed-the-font-to -a-unicode-enabled-one

這是那里的兩個相關文件:

templates目錄中名為pdfstylefonts.css的 Django-template-stylesheet:

{% load static %}
@font-face {
    font-family: "Calibri";
    src: url({% static "fonts/calibri.ttf" %});
}

和 html 模板:

{% load static %}
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        {% include "pdfstylefonts.css" %}
    </style>
    <link rel='stylesheet' href='{% static "pdfstyle.css" %}'/>
</head>
<body>
    <h1>Λίστα βιβλίων</h1>
    <img src='{% static "pony.png" %}' />
    <table>
        <tr>
            <th>ID</th><th>Title</th><th>Cover</th>
        </tr>
        {% for book in books %}
            <tr>
                <td>{{ book.id }}</td><td>{{ book.title }}</td><td><img src='{{ book.cover.url }}' /></td>
            </tr>
        {% endfor %}
    </table>
</body>
</html>

請注意 css-django-template(通過<style>{% include "pdfstylefonts.css" %}</style>包含它)和普通 css 文件(通常使用<link rel='stylesheet' href='{% static "pdfstyle.css" %}'/>包含它)之間的區別<link rel='stylesheet' href='{% static "pdfstyle.css" %}'/> )。

暫無
暫無

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

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