我正在尝试在我的 python django 应用程序中加载 Font Awesome 图标的本地副本。
我的模板 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>
改编自 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
然而,这些图标没有被提供。如果我使用以下内容,它会起作用:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
最佳答案
我遇到过同样的问题。 Django 无法找到 webfonts 目录 - 当我运行 python manage.py runserver
时,我可以看到 404
对 Font Awesome 文件的响应。
在您的 all.css
内文件,在底部,你应该看到一些 src: url(../webfonts/..)
属性。将这些更改为 src: url("/static/webfonts/..)
.当您runserver
你应该看到一个 200
响应而不是 404
,表示已找到您的文件。
如果仍有错误,请检查控制台。右键单击图标(它应该在的位置),单击“检查”,单击“控制台”(在 Chrome 中;或任何与您的浏览器等效的浏览器)
关于python - 在 python django 应用程序中使用 font-awesome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605360/