I have been using the AbstractLinkPage model from wagtailmenus in my site for a while now. I find it quite useful.
Source: wagtailmenus AbstractLinkPage model
My site is building cards from children pages and adds banner images from children pages automatically to these cards. Some of the children pages are LinkPages.
How can I extend the subclassed LinkPage (from AbstractLinkPage) to add a generic image which can be used as a card image?
I have added the field:
class LinkPage(AbstractLinkPage):
card_image = models.ForeignKey(
get_image_model_string(),
null=True,
blank=True,
related_name='+',
on_delete=models.SET_NULL,
)
content_panels = AbstractLinkPage.content_panels + [
ImageChooserPanel("card_image"),
]
However the image card_image field does not show in the admin interface.
Any help to move forward is appreciated.
Wagtailmenus uses it's own tabbed interface (in wagtailmenus.panels):
linkpage_tab = ObjectList(
linkpage_panels, heading=_("Settings"), classname="settings"
)
linkpage_edit_handler = TabbedInterface([linkpage_tab])
So what you have to do is to create a new ObjectList:
from wagtailmenus.panels import linkpage_panels
from wagtail.admin.edit_handlers import ObjectList, TabbedInterface
my_linkpage_tab = ObjectList(
linkpage_panels + [ImageChooserPanel("card_image")],
heading=_("Settings"), classname="settings"
)
.. and add it to your class:
class LinkPage(AbstractLinkPage):
[..]
edit_handler = TabbedInterface([my_linkpage_tab])
Related
Edit: Seems like I can't add images :(, added links to imgur instead.
I'm trying to implement settings in order to enter social media accounts.
#register_setting
class SocialMediaSettings(BaseGenericSetting):
discord_url = models.URLField(
help_text="Link used."
)
discord_name = models.CharField(
max_length=100,
help_text="Name to be displayed."
)
gitlab_url = models.URLField(
help_text="Link used."
)
gitlab_name = models.CharField(
max_length=100,
help_text="Name to be displayed."
)
panels = [
FieldPanel("discord_url"),
FieldPanel("discord_name"),
FieldPanel("gitlab_url"),
FieldPanel("gitlab_name")
]
Which works totally fine, but as it feels like a super bad idea to do it this way, I wanted to use a streamfield like this:
#register_setting
class SocialMediaSettings(BaseGenericSetting):
body = StreamField([
("social_account", blocks.SocialBlock()),
], null=True, blank=True, use_json_field=True)
panels = [
FieldPanel("body")
]
class SocialBlock(blocks.StructBlock):
name = blocks.CharBlock(
max_length=100,
help_text="Name used for tooltips."
)
url = blocks.URLBlock(
help_text="URL"
)
Image of wagtail admin (image)
This way it shows up partly. In the admin panel I can only enter the name, not the url. And saving doesn't work eighter (saving will reset what I entered in the admin panel).
Adding the SocialBlock to a normal page works as expected:
SocialBlock working in another Page (image)
Which is why I ran out of ideas. Are streamfields not supported in snippets / settings? (the above exapmles are done with #register_setting but I tried #register_snippet with the same result.)
Any idea how I could get this up and running?
Wagtail 4.0
Django 4.1
Well, wagtail itself just showed me the solution :x
It was a bug in Wagtail 4.0, which is solved in 4.0.1
Release Notes Wagtail 4.0.1
I've been around the Internet the whole day reading about the following issue, in wagtail if I registered the following model for translation like this:
class RecipientsPage(Page):
intro = RichTextField(null=True, blank=True)
banner_image = models.ForeignKey(
"wagtailimages.Image",
on_delete=models.SET_NULL,
related_name="+",
null=True,
blank=False,
help_text=_("the Image shouldn't exceed ") + "1350 * 210",
)
content_panels = Page.content_panels + [
FieldPanel("intro"),
ImageChooserPanel("image"),
]
this is how I registered the model:
#register(RecipientsCountriesPage)
class RecipientsCountriesPage(TranslationOptions):
fields = ("intro",)
It causes a problem, because like this I'll have two slugs following the two titles (The original English one and the Arabic translated one), if I change the Arabic slug manually to equal the English one it'll work, but it's not efficient to do so for each page manually
I've read about the issue a lot like in here:
https://github.com/infoportugal/wagtail-modeltranslation/issues/195
I've found also the following question with no answer
How do you translate the slug value of a page?
I've also read that I can override some of the wagtail Page methods but without further explanation and I'm a bit lost, what's the best way to overcome this issue?
I did it using Django signals
#receiver(pre_save)
def set_arabic_slug_on_new_instance(sender, instance, **kwargs):
if isinstance(instance, Page):
instance.slug_ar = instance.slug_en
In Wagtail, is it possible to filter pages that show up within the PageChooserPanel page?
For example if I'm setting a link for a french page, I would only want to see pages marked as French. Something like the fake example below:
class MyPage(Page):
french_link = models.ForeignKey(
Page,
null=True,
blank=True,
related_name='+',
on_delete=models.SET_NULL
)
panels = [
# something like this that can
# limit the pages to only ones where lang equals fr
PageChooserPanel('french_link', filter=limit_by_lang),
]
def limit_by_lang(query):
return query.get(lang='fr')
Thanks.
Some time ago I stopped using #register_snippet to decorate Snippets. This takes the Snippet out of the snippets section of admin.
Instead I used the wagtail_hooks.py to show the snippet directly in the left admin panel for user convenience. See below. This works nicely as the user can go directly to the snippet and you can also alter the displayed fields and ordering of fields - nice.
So in the below example I removed the line that says #register_snippet. What's the catch? The SnippetChooserPanel does not work! Later I was building a complex model and the SnippetChooserPanel did not work. I wasted quite a bit of time thinking the problem was in the complexity of my model. I want to save others' time!
wagtail_hooks.py:
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from wagtail.wagtailsnippets.models import register_snippet
from demo.models import Advert
class AdvertAdmin(ModelAdmin):
model = Advert
modeladmin_register(AdvertAdmin)
Here is the snippet example from Wagtail: snippets
#register_snippet #<------- Source of issue (I removed this line!)
#python_2_unicode_compatible # provide equivalent __unicode__ and __str__ methods on Python 2
class Advert(models.Model):
url = models.URLField(null=True, blank=True)
text = models.CharField(max_length=255)
panels = [
FieldPanel('url'),
FieldPanel('text'),
]
def __str__(self):
return self.text
class BookPage(Page):
advert = models.ForeignKey(
'demo.Advert',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
SnippetChooserPanel('advert'),
# ...
]
If you make your Snippets editable via modelAdmin you still need to apply the decorator #register_snippet. Otherwise the chooser panel route/view won't be available. This view is requested by the ajax request fired on SnippetChooser modal open. Missing #register snippet will trow a 404.
You can register menu items via construct_main_menu hook. You can use the same hook to remove exiting menu-items. If you do not want the 'Snippets' menu item remove it. In wagtail_hooks.py:
#hooks.register('construct_main_menu')
def hide_snippet(request, menu_items):
menu_items[:] = [item for item in menu_items if item.name != 'snippets']
The solution is always use #register_snippet decorator otherwise the SnippetChooserPanel doesn't work!
#register_snippet
#python_2_unicode_compatible
class Advert(models.Model):
url = models.URLField(null=True, blank=True)
text = models.CharField(max_length=255)
panels = [
FieldPanel('url'),
FieldPanel('text'),
]
def __str__(self):
return self.text
I'm totally new to Wagtail/ Django.
Here's what I am trying to achieve:
I'd like to have an ability in the backend of my Wagtail CMS install to create 'pages' or 'posts' that follow a strict template.
The template would have custom fields like 'header' and aim content' etc.
I'm sure that this is possible, I'd just be interested to know how I'd go about achieving this?
For example, does anyone know if Wagtail has a plugin or other to enable this?
Thanks for all help/ direction.
You want to create pages and posts that follow a strict template: that's exactly what Django and Wagtail let you do. But there's one catch: Wagtail takes this a step further and lets you move entire sections of a page — these are called Streamfields. It's an amazing feature, to be honest.
Here's an example to get you started (note: this is untested and not linted)
# -*- coding: utf-8 -*-
"""Basic Page model."""
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from your_custom_app.streams import streamfields
class BasicPage(Page):
"""A basic page class."""
template = "templates/pages/basic_page.html"
parent_page_type = ["pages.HomePage", "pages.BasicPage"]
subpage_types = ["pages.BasicPage"]
header = models.CharField(max_length=100)
content = StreamField(
('streamfield_name', streamfields.CustomStreamfield()),
# ... More streams
null=True,
blank=True,
)
# Other additional fields you want on your page.
# Panels are how you lay out your pages in the /admin/
content_panels = [
FieldPanel("title", classname="full title"),
FieldPanel("header"),
# FieldPanel("other_fields"),
StreamFieldPanel("content"),
]
settings_panels = Page.settings_panels + [] # Custom settings panel
promote_panels = Page.promote_panels + [] # Custom promote panel
class Meta:
"""Meta information."""
verbose_name = "Basic Page"
verbose_name_plural = "Basic Pages"
You can also download and setup the Wagtail Bakery Demo, it has a lot of great examples in it.