在 python django 应用程序中使用 font-awesome

Joe*_*hew 5 python django font-awesome

我正在尝试在我的 python django 应用程序中加载字体真棒图标的本地副本。

我的模板 base.html 包含:

{% load static %}
<!doctype html>
<html lang="en">
<head>   
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'appointments/css/all.css' %}" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
    <title>{% block title %}Hello, world!{% endblock %}</title>
</head>
Run Code Online (Sandbox Code Playgroud)

改编自https://docs.djangoproject.com/en/2.0/howto/static-files/

我的应用程序的目录结构是:

joel@hp:~/myappointments$ tree
.
??? appointments
?   ??? admin.py
?   ??? apps.py
?   ??? __init__.py
?   ??? migrations
?   ?   ??? __init__.py
?   ??? models.py
?   ??? static
?   ?   ??? appointments
?   ?       ??? css
?   ?       ?   ??? all.css
?   ?       ??? webfonts
?   ?           ??? fa-brands-400.eot
?   ?           ??? fa-brands-400.svg
?   ?           ??? fa-brands-400.ttf
?   ?           ??? fa-brands-400.woff
?   ?           ??? fa-brands-400.woff2
?   ?           ??? fa-regular-400.eot
?   ?           ??? fa-regular-400.svg
?   ?           ??? fa-regular-400.ttf
?   ?           ??? fa-regular-400.woff
?   ?           ??? fa-regular-400.woff2
?   ?           ??? fa-solid-900.eot
?   ?           ??? fa-solid-900.svg
?   ?           ??? fa-solid-900.ttf
?   ?           ??? fa-solid-900.woff
?   ?           ??? fa-solid-900.woff2
?   ??? templates
?   ?   ??? appointments
?   ?       ??? base.html
?   ?       ??? createappointment.html
?   ?       ??? doctor.html
?   ?       ??? doctors.html
?   ?       ??? home.html
?   ?       ??? index.html
?   ?       ??? navbar.html
?   ?       ??? patient.html
?   ?       ??? patients.html
?   ??? tests.py
?   ??? urls.py
?   ??? using shell.txt
?   ??? views.py
??? appointments.ipynb
??? db.sqlite3
??? manage.py
??? myappointments
?   ??? __init__.py
?   ??? settings.py
?   ??? urls.py
?   ??? wsgi.py
Run Code Online (Sandbox Code Playgroud)

然而,这些图标没有被提供。如果我使用以下内容,它会起作用:

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
Run Code Online (Sandbox Code Playgroud)

don*_*don 1

我遇到过同样的问题。Django 无法找到 webfonts 目录 - 当我运行时python manage.py runserver,我可以看到404font-awesome 文件的响应。

在文件的all.css底部,您应该会看到一些src: url(../webfonts/..)属性。将这些更改为src: url("/static/webfonts/..). 当您runserver应该看到200响应而不是 时404,表明您的文件已找到。

如果仍然有错误,请检查控制台。右键单击图标(应该在的位置),单击“检查”,单击“控制台”(在 Chrome 中;或您的浏览器的等效项)