Git Product home page Git Product logo

django-embed9's Introduction

django-embed9

django-embed9 allows you to create widgets from your model objects and embed them on external websites.

Requirements

Installation

  • Install via pip:

    pip install -e git+git://github.com/adawg4/django-embed9.git@master#egg=django-embed9
  • Add embed9 to your INSTALLED_APPS
  • Add url pattern to your urls.py:

    path('embed/', include(('embed9.urls','embed9'), namespace="embed9")),
  • Create a site in your database and set the SITE_ID setting
  • (Optional) Add the following line to your robots.txt file:

    Disallow: /embed/widget/

Usage

For every model that you want to make embeddable, create a class extending embed9.main.Embeddable in embed.py file inside your app folder. Class name has to consists of your model name and a word Embed.

For example model Image:

# embed.py
from embed9.main import Embeddable

class ImageEmbed(Embeddable):
    pass

In order to provide your own template of the widget, add widget_template to the class (or define get_widget_template() method):

class ImageEmbed(Embeddable):
    widget_template = 'myapp/mywidget.html'

Inside the template you can access a context variable named after your model (but in lower case). In this example it would be {{ image }}.

By default, defined widget template will render as an iframe on external websites.

To display a widget preview or raw embed code for an object in the template use the following:

{% load embed %}

{% widget_code myobject %}
{% widget_preview myobject %}

To display an URL to the preview/customization page you can use templatetag widget_preview_url:

{% widget_preview_url myobject %}

django-embed9 provides a simple demo with example usage. To install it from the console, navigate to embed9/demo directory and execute make install command. To run it, type make.

Customization

Templates

You may want to change the default templates for embed code or for the JavaScript loader. To do that, specify these templates in your Embeddable class:

class ImageEmbed(Embeddable):
    code_template = 'myapp/mycode.html'
    loader_template = 'myapp/myloader.js'

Again, if you need to provide these names dynamically, define get_code_template() and get_loader_template() methods.

Remember to use {{ widget_name }} as shown in the default templates. It is required if you want to allow many widgets on a single page.

Of course, sometimes JavaScript loader is not necessary. If this is the case, you will just need to create a code template similar to:

<iframe src="{{ domain }}{{ iframe_url }}"></iframe>

Parameters

You may want to add some custom parameters such as widget size or color and allow your users to adjust them.

To define such parameters, create a form class and tell django-embed9 where it should look for it:

# embed.py
class ImageEmbed(Embeddable):
    form_class = MyAwesomeForm
    form_template = 'myapp/myform.html'

Example form:

# forms.py
class MyAwesomeForm(forms.Form):
    size = forms.IntegerField(max_value=500, min_value=100, initial=200)
    color = forms.CharField(max_length=6)

Now all these parameters are going to be passed to the templates as a dictionary named {{ params }}:

<p>Size of this widget should be: {{ params.size }}</p>

Widget customization is available on the preview page. To display a link to it, write:

{% load embed %}
{% widget_preview_url myobject %}

You can also pass the parameters directly to templatetags:

{% load embed %}
{% widget_code myobject size=1000 %}

Caching

By default, widgets (iframes and javascript loaders) are cached for an hour. In order to adjust this behaviour, set the WIDGET_CACHE_TIME setting:

WIDGET_CACHE_TIME = 24 * 60 * 60

In progress

  • oEmbed API.

Notes

  • Tested with Python 3.6.4 d Django 2.2.7

django-embed9's People

Contributors

9dev avatar adawg4 avatar

Stargazers

 avatar  avatar

Watchers

 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.