Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wagtail, tu aurais pu t'appeler Swagtail.
# templatetags/block_tags.py
import re
register = template.Library()
@register.inclusion_tag('tags/embed_tweet.html', takes_context=True)
def tweet_embed(context, url):
return {
'url': '{}'.format(url),
'request': context['request'],
}
# blocks.py
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailcore import blocks
class TweetBlock(blocks.StructBlock):
url = blocks.URLBlock(help_text=_('Tweet URL'))
class Meta:
icon = 'twitter'
<style>
.twitter-tweet {
display: block;
margin: auto;
}
</style>
<blockquote class="twitter-tweet">
<a href="{{ url }}"> Chargement du tweet</a>
</blockquote>
<!-- Don't forget to insert the twitter script ! -->
<!-- <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script> -->
# models.py
class BlogPost(Page):
cover_picture = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text=_('Choose the cover picture')
)
body = StreamField([
('paragraph', blocks.RichTextBlock(
label=_('paragraphe'),
icon='pilcrow',
help_text=_('Insert a paragraph'),
)),
('tweet', TweetBlock(
label=_('Tweet URL'),
help_text=_('Insert a Tweet')
)),
])
content_panels = Page.content_panels + [
ImageChooserPanel('cover_picture'),
StreamFieldPanel('body'),
]
search_fields = Page.search_fields + (
index.SearchField('body'),
)
# models.py
class BlogPost(Page):
cover_picture = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text=_('Choose the cover picture')
)
body = RichTextField()
content_panels = Page.content_panels + [
ImageChooserPanel('cover_picture'),
FieldPanel('body'),
]
search_fields = Page.search_fields + (
index.SearchField('body'),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment