django-fontawesome is a Django app that provides a couple of Fontawesome/Django related utilities, namely:
an IconField to associate Fontawesome icons with model instances
templatetags to render Fontawesome icons
also included:
admin support for the IconField
fr locale translation
Requirements
PyYAML
Select2 (included)
JQuery (uses django's jquery in admin panel)
Settings
By default, django-fontawesome ships with and uses the lastest fontawesome release.
You can configure django-fontawesome to use another release/source/cdn by specifying:
# default uses locally shipped version at 'fontawesome/css/font-awesome.min.css'
FONTAWESOME_CSS_URL = '//cdn.example.com/fontawesome-min.css' # absolute url
FONTAWESOME_CSS_URL = 'myapp/css/fontawesome.min.css' # relative url
You can also tell it the fontawesome prefix, which as of right now is 'fa', using:
FONTAWESOME_PREFIX = 'bg' # default is 'fa'
Installation / Usage
Install via pip:
pip install django-fontawesome
Add 'fontawesome' to your installed apps setting like this:
INSTALLED_APPS = (
...
'fontawesome',
)
Import and use the IconField:
from fontawesome.fields import IconField
class Category(models.Model):
...
icon = IconField()
Here's what the widget looks like in the admin panel:
You can then render the icon in your template like this:
{% for category in categories.all %}
{% if category.icon %}
{{ category.icon.as_html }}
{% endif %}
{% endfor %}
django-fontawesome ships with two template tags, fontawesome_stylesheet and fontawesome_icon.
the former inserts a stylesheet link with a pre-configured href according to the FONTAWESOME_CSS_URL setting
the latter renders icons, and accepts the following optional keywords arguments: large, spin, fixed, li, border: (true/false), rotate: (90/180/270), title: (string)
you can also colorize an icon using the color='red' keyword argument to the fontawesome_icon template tag
请发表评论