GithubHelp home page GithubHelp logo

isabella232 / django-embedded-media Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paylogic/django-embedded-media

0.0 0.0 0.0 93 KB

Inline CSS and JS in your form media

License: MIT License

Python 100.00%

django-embedded-media's Introduction

django-embedded-media

https://travis-ci.org/dmpayton/django-embedded-media.png
Author:Derek Payton
Version:0.1.0
License:MIT

Ever run into a situation where it would just be so incredibly handy to be able to render inline CSS or JS in your Django form media?

This lets you do that.

It all started with ticket #13978, which I championed for a while. However, I no longer think this is a good feature to have in Django. After using the #13978 patch for a while, I've come to realize that it's just too damn easy to rely on it too much. No one wants to see a two-dozen line string of jQueryfied JS in the middle of their Django form class.

However, if you've got the need for spee^H^H^H^Hembedded media -- and you think you're disciplined enough to not start writing all your CSS and JS as strings in your .py files -- then django-embedded-media makes it possible.

Installation

Use pip, obviously:

$ pip install django-embedded-media

Usage

There's no special configuration needed. It's a Django app, technically, but you don't need to put it into INSTALLED_APPS. Just install the package and start embedding your styles and scripts.

Works on forms:

>>> from django import forms
>>> import embedded_media as emb
>>>
>>> class MyForm(forms.Form):
...     class Media:
...         css = {'all': (emb.CSS('.mywidget { display: none; }'),)}
...         js = (emb.JS('init_mywidget();'),)

>>> print MyForm.media
<style type="text/css" media="all">.mywidget { display: none; }</style>
<script type="text/javascript">init_mywidget();</script>
>>>

Works on form widgets:

>>> from django import forms
>>> import embedded_media as emb
>>>
>>> class MyWidget(forms.TextInput):
...     class Media:
...         css = {'all': (emb.CSS('.mywidget { display: none; }'),)}
...         js = (
...             'whizbang.js',
...             emb.JS('init_mywidget();'),
...         )

>>> print MyWidget().media
<style type="text/css" media="all" >.mywidget { display: none; }</style>
<script type="text/javascript" src="/static/whizbang.js"></script>
<script type="text/javascript">init_mywidget();</script>
>>>

Works as a dynamic media property:

>>> from django import forms
>>> import embedded_media as emb
>>>
>>> class MyForm(forms.Form):
...     def _media(self):
...         return Media(
...             css={'all': (emb.CSS('.mywidget { display: none; }'),)},
...             js=(emb.JS('init_mywidget();'),)
...         )
...     media = property(_media)

>>> print MyForm().media
<style type="text/css" media="all">.mywidget { display: none; }</style>
<script type="text/javascript">init_mywidget();</script>
>>>

Testing

The tests require Django, coverage, and pep8, which are conveniently listed in requirements.txt:

$ pip install -r requirements.txt
$ python setup.py test

django-embedded-media's People

Contributors

dmpayton avatar zbyte64 avatar

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.