GithubHelp home page GithubHelp logo

redouane / django-fontawesome Goto Github PK

View Code? Open in Web Editor NEW
62.0 4.0 73.0 1.64 MB

a django app that provides a couple of fontawesome/django related utilities

License: BSD 2-Clause "Simplified" License

Python 78.61% JavaScript 21.39%

django-fontawesome's Introduction

django-fontawesome

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

  1. Install via pip:

    pip install django-fontawesome
    
  2. Add 'fontawesome' to your installed apps setting like this:

    INSTALLED_APPS = (
        ...
        'fontawesome',
    )
    
  3. 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:

admin-widget

  1. 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 %}
    
  2. 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

    • example usage:

      {% load fontawesome %}
      
      <head>
        {% fontawesome_stylesheet %}
        ...
      </head>
      
      {% fontawesome_icon 'user' color='red' %}
      
      {% fontawesome_icon 'star' large=True spin=True %}
      
      <ul class="fa-ul">
         <li> {% fontawesome_icon 'home' rotate=90 li=True %} One</li>
      </ul>
      
  3. profit!!!

changelog

1.0 - May 10, 2018

  • django 1.11 compability
  • usage of yaml's safe_load instead of insecure load
  • new pypi release (1.0), is now considered a stable release

0.3.1 - Dec 19, 2016

  • added unicode literals import for cases where icon title attribute uses non ascii chars

Nov 28, 2016

  • now suppots django 1.8+
  • python3 supported, supposedly
  • relative import issues fixed
  • updated icon mapping for fontawesome 4.7
  • tabular and stacked inlines now supported
  • other small fixes
  • new PyPI release (0.3)

Jan 28, 2016

  • updated icon mapping file (icons.yml) for fontawesome 4.5.0
  • new keyword arg for fontawesome_icon template tag: title

Dec 17, 2015

  • Updated locally shipped fontawesome to 4.5.0
  • fontawesome_icon's output is now marked safe

Sep 11, 2015

  • Updated locally shipped fontawesome to 4.4.0

Feb 27, 2015

  • added two new keyword argument to the fontawesome_icon template tag, color and border
  • FONTAWESOME_PREFIX setting is now taken into account when rendering icons using the fontawesome_icon template tag

django-fontawesome's People

Contributors

atiberghien avatar bittner avatar dersphere avatar jelouemoncampingcar avatar mitzam avatar mjamro avatar redouane avatar sidneijp avatar thijstriemstra avatar zoidyzoidzoid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-fontawesome's Issues

Parent element title is suppressed if no title is given to the icon

I suspect this will be related to #13.

Simply put, if you don't supply a title in a call to fontawesome_icon, the resulting HTML looks something like this:

<i title="" class="fa fa-square-o fa-fw"></i>

Because of the title="", any title set by the parent element is "ignored" by the browser. This means it's not possible to do things like this:

<button title="Here is some help">
  Press me! {% fontawesome_icon "square-o" fixed=True %}
</button>

and have the title text appear in a tooltip for the whole of the button -- it disappears if you mouse over the icon.

django-fontawesome incompatible with latest Django release 3.0

I tried upgrading a Django project to Django 3.0 and got the following error:

File ".../lib/python3.7/site-packages/fontawesome_5/app_settings.py", line 4, in <module>
    from django.contrib.staticfiles.templatetags.staticfiles import static
ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'

As mentioned in the release notes:

  • The staticfiles and admin_static template tag libraries are removed.
  • django.contrib.staticfiles.templatetags.staticfiles.static() is removed.

Django 3 import error

I get the following when am trying to run server on my local machine after setting up this module

django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'fontawesome.templatetags.fontawesome': No module named 'django.contrib.staticfiles.templatetags'

Font isn't loading !!

I got the following settings/ configuration:

  1. pip install django-fontawesome
  2. settings.py:
    INSTALLED_APPS = ( 'fontawesome', )
  3. added font-awesome.min.css under css/ and is successfully loaded using django-pipeline
  4. In base.html:
    at top I got: {% load fontawesome %}
    in body I got: {% fontawesome_icon 'user' %}

Font awesome fonts isn't loaded. , what I might be missing..?

IconBlock class

Could you add an IconBlock chooser to use in Streamfields, as right now we are only able to use IconField ?!

Can't see icons in admin panel

Hi,
i'm new to Django and trying to use this nice lib, I can't see the icons in admin panel:
screen shot 2017-05-17 at 11 06 00

Any ideas what is wrong ?
Thanks
Shani

How to change the icon size

Hello,
I'm working with Django and I would like to understand how to make a different size for the icon.
I would like to have a bigger icon.

I used your package and the icon is shown which is home in my case.
However, I don't understand how I can manipulate the size.

Thank you for help

Easy way to include icons in list_display?

Obvious way is to add the css link to your admin base_site.html and write a method each model or model admin to render as_html.

I'm trying to think of a more elegant way that requires less configuration.

category.icon or category.icon.as_html not rendering

Hi,

I've tried both scenarios, of using the icon with predefined css class placing the tag inside it, but it shows in css inspector, althtout the browser does not render.

Do you have a better solution to fix this problem?

as_html not working

Inside my template, I have this code block:

{% if profile.icon_bookmark %}
    <td class="text-center">{{ profile.icon_bookmark.as_html }}</td></td>
{% else %}
    hello
{% endif %}

Nothing show in my rendered html file. I don't know why.

fontawsome tag is not working in templates

python

Environment:

Request Method: GET
Request URL: http://localhost:9900/

Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('grappelli',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fontawesome',
'bootstrap3',
'website')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

Template error:
In template /home/niranj/Documents/personal/niranj-website/website/templates/website/index.html, error at line 5
Invalid block tag: 'fontawesome_icon', expected 'endblock'
1 : {% extends 'master.inc' %}

2 :

3 : {% block content %}

4 :

hai

5 : {% fontawesome_icon 'user' %}

6 :

7 :

8 :

9 : {% endblock %}

10 :

11 :

Traceback:
File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response

  1.                 response = wrapped_callback(request, _callback_args, *_callback_kwargs)
    
    File "/home/niranj/Documents/personal/niranj-website/website/views.py" in index
  2. return render(request, 'website/index.html', {})
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/shortcuts.py" in render
  3. return HttpResponse(loader.render_to_string(_args, *_kwargs),
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  4.     t = get_template(template_name, dirs)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
  5. template, origin = find_template(template_name, dirs)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
  6.         source, display_name = loader(name, dirs)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in call
  7.     return self.load_template(template_name, template_dirs)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in load_template
  8.         template = get_template_from_string(source, origin, template_name)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader.py" in get_template_from_string
  9. return Template(source, origin, name)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in init
  10.     self.nodelist = compile_string(template_string, origin)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in compile_string
  11. return parser.parse()
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in parse
  12.                 compiled_result = compile_func(self, token)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader_tags.py" in do_extends
  13. nodelist = parser.parse()
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in parse
  14.                 compiled_result = compile_func(self, token)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/loader_tags.py" in do_block
  15. nodelist = parser.parse(('endblock',))
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in parse
  16.                 self.invalid_block_tag(token, command, parse_until)
    
    File "/home/niranj/Envs/niranj/local/lib/python2.7/site-packages/django/template/base.py" in invalid_block_tag
  17.             (command, get_text_list(["'%s'" % p for p in parse_until])))
    

Exception Type: TemplateSyntaxError at /
Exception Value: Invalid block tag: 'fontawesome_icon', expected 'endblock'
~~

Implicit relative imports break python3 support.

Import mechanism changed in python3 and implicit relative imports won't work any more.

from forms import IconFormField
ImportError: No module named 'forms'

from forms import IconFormField needs to become from .forms import IconFormField, similar in other places.

This is a trivial change and I will send a PR very soon. The change is backwards compatible, you can read more about it here.

No space before style attribute

Hi! I put this code into my template:
{% fontawesome_icon 'user' color='gray' %}

Here's the output:
<i title="" class="fa fa-user"style="color:gray;">

As you can see there's no space between class and style attributes.
I'm using Django 1.9.6 for my project.

Anyway, thanks for the package, it really helpful.

Add some support for additional icons

Hi,
I've installed django-fontawesome from pip on my dev platform, and I'ld like to use in my prod server.

I do need the map icon from fontawesome, which looks like not to be in the icons.yml file.
I can manually update the file, but I loose those changes when updating the prod venv with pip.

I've 2 questions :

  • could you update the module to update pypi with this ?
  • is it possible to re-generate the file from actual fontawesome website and to put it somewhere to the project file tree ? maybe through a management command ?

Thanks for your great job !

collectstatic fail with ManifestStaticFilesStorage

when the django STATICFILES_STORAGE is set to 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage', the collect static fail because select2/select2.css refer to a non existing image: 'select2/select2x2.png'

here is the stack

Post-processed 'fontawesome/css/font-awesome.min.css' as 'fontawesome/css/font-awesome.min.04425bbdc624.css'
Post-processing 'fontawesome/select2/select2.css' failed!

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
    collected = self.collect()
  File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 120, in collect
    raise processed
ValueError: The file 'fontawesome/select2/select2x2.png' could not be found with <django.contrib.staticfiles.storage.ManifestStaticFilesStorage object at 0x7f61e9c0e790>.

Add unicode_literals import

Hi,
Is it possible to add:

from __future__ import unicode_literals

into the django-fontawesome/fontawesome/templatetags/fontawesome.py to enable mixed unicode / ascii formatting...

Thanks !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.