django-embed-video/docs/examples.rst
Juda Kaleta 07de8a3af8 Closes #11.
Major changes:

- filter ``embed`` is replaced by tag ``video``.
- removed caching for whole backend, caching properties instead.
- HTTP or HTTPS is set by context.is_secure() (just for ``video`` tag).
- backends can be HTTP only (``allow_https = False``).

Minor changes:

- changed theme of docs to RTD theme.
- help about testing HTTPS on development server.
- added and modified tests
2013-12-21 17:18:47 +01:00

127 lines
3 KiB
ReStructuredText

Examples
==============================================
Template examples
##############################################
.. highlight:: html+django
First you have to load the `embed_video_tags` template tags in your template:
::
{% load embed_video_tags %}
Embedding of video:
::
{# you can just embed #}
{% video item.video 'small' %}
{# or use variables (with embedding, too) #}
{% video item.video as my_video %}
URL: {{ my_video.url }}
Thumbnail: {{ my_video.thumbnail }}
Backend: {{ my_video.backend }}
{% video my_video 'small' %}
{% endvideo %}
Default sizes are ``tiny`` (420x315), ``small`` (480x360), ``medium`` (640x480),
``large`` (960x720) and ``huge`` (1280x960). You can set your own size:
::
{% video my_video '800x600' %}
.. note::
We recommend to use `sorl-thumbnail
<http://sorl-thumbnail.readthedocs.org/en/latest/>`_ to `change
<http://sorl-thumbnail.readthedocs.org/en/latest/examples.html#template-examples>`_
thumbnail size.
Model examples
###############################################
.. highlight:: python
Using the EmbedVideoField provides you validation of URLs.
::
from django.db import models
from embed_video.fields import EmbedVideoField
class Item(models.Model):
video = EmbedVideoField() # same like models.URLField()
Admin mixin examples
###############################################
Use AdminVideoMixin in admin.py.
::
from django.contrib import admin
from embed_video.admin import AdminVideoMixin
from .models import MyModel
class MyModelAdmin(AdminVideoMixin, admin.ModelAdmin):
pass
admin.site.register(MyModel, MyModelAdmin)
Custom backends
###############################################
If you have specific needs and default backends don't suits you, you can write
your custom backend.
``my_project/my_app/backends.py``::
from embed_video.backends import VideoBackend
class CustomBackend(VideoBackend):
re_detect = re.compile(r'http://myvideo\.com/[0-9]+')
re_code = re.compile(r'http://myvideo\.com/(?P<code>[0-9]+)')
allow_https = False
pattern_url = '{protocol}://play.myvideo.com/c/{code}/'
pattern_thumbnail_url = '{protocol}://thumb.myvideo.com/c/{code}/'
You can also overwrite :py:class:`~embed_video.backends.VideoBackend` methods,
if using regular expressions isn't well enough.
``my_project/my_project/settings.py``::
EMBED_VIDEO_BACKENDS = (
'embed_video.backends.YoutubeBackend',
'embed_video.backends.VimeoBackend',
'embed_video.backends.SoundCloudBackend',
'my_app.backends.CustomBackend',
)
Low level API examples
###############################################
You can get instance of :py:class:`~embed_video.backends.VideoBackend` in your
python code thanks to :py:func:`~embed_video.backends.detect_backend`:
::
from embed_video.backends import detect_backend
my_video = detect_backend('http://www.youtube.com/watch?v=H4tAOexHdR4')