Wagtail CMS: Adding Custom StreamField Logic

  Рет қаралды 6,435

Coding For Everybody

Coding For Everybody

5 жыл бұрын

There will be times when you need to provide multiple optional fields in a StreamField, and naturally we lean towards using logic in the template. In this video we're going to learn how to pull out template logic and use Python instead. In this example we'll be using a StructValue and StructBlock to return a single URL in the template even though the StreamField has a PageChooserBlock and a URLBlock (pick one and return it).
Tutorial: learnwagtail.com/tutorials/ad...
GitHub Commit: github.com/CodingForEverybody...
Learn Wagtail from scratch with the official Wagtail for Beginners Course
learnwagtail.com/wagtail-for-...
Used in this video: Wagtail 2.4, Python 3.7, Django 2.1.5 #Wagtail #Django #Python

Пікірлер: 19
@cheryl_jones
@cheryl_jones 3 жыл бұрын
Awesome, was just able to create a 'related posts' block using this tutorial! Thanks for the great content :)
@zero-lpds
@zero-lpds 5 жыл бұрын
Extremely useful and simplified tutorial that guided me to the value_class and how I can effectively use it. Thanks! one observation tough: it caught my eye if / elif blocks, when you're using them to conditionally return stuff - simply two ifs would do the same and they could be easier read like so: if page_url: return page_url if custom_url: return cutsom_url return None no? also less processing power required.
@CodingForEverybody
@CodingForEverybody 5 жыл бұрын
Yep, you're right, that would work. I'm not sure about less processing power, but I'd assume you're correct about that as well. Good catch!
@zero-lpds
@zero-lpds 5 жыл бұрын
@@CodingForEverybody, thinking about it, that (less processing) might not be true actually, but better code quality for sure.
@jasong1639
@jasong1639 4 жыл бұрын
enjoying this course. Just wondering why we use separate internal and external url fields but in the richtext field their is one link button that covers internal, external and email links. why isnt that available outside the richtext field?
@hazhohuman
@hazhohuman 4 жыл бұрын
hi kalob, thanks again, just asking if we can collect all links in one app, for example by using context or any other better way, please..! note: of course I mean after the logic above is returning only one of the both options of input/chooser (only the returned value to be collected in one app/model for entire project )
@tanveerahmed1040
@tanveerahmed1040 5 жыл бұрын
Thank you for making Tutorials on such Awesome CMS Please Create Video on Multi columns and more logic and dynamic to render in template Grid System like one third or two third grid Please.
@CodingForEverybody
@CodingForEverybody 5 жыл бұрын
Hi Tanveer, I think you're talking about CSS Grid on the frontend, right? If so, that's a little out of scope for this course - I want this course to focus mainly on the Wagtail site of things. Generally we give the Admin (content editors) the ability to enter content, but the template is somewhat static. So we can give them options for "left column title" and "right column title". Or if you need unlimited columns, you'd use a ListBlock in a StreamField or an Orderable on a Page, and let HTML+CSS handle the styling and columns. Although there is a way to add multiple columns in the Wagtail Admin! I'll add that to my list of videos to make.
@tanveerahmed1040
@tanveerahmed1040 5 жыл бұрын
Thank You for reply I have search a lot about grid system in wagtail I have got coderedcms have you heard about it? but it has only bootstrap design I can customize but it has a lot of features I will use them in my future projects as clients need but now I want simple way to do grid like coderedcms when I choose column size from admin panel it will render in template dynamically and logical display in frontend whatever I chosen sizes maybe Half - 1/2 column Thirds - 1/3 column Thirds - 2/3 column Twelfths - 7/12 Twelfths - 5/12 column and When I choose left column size Twelfths - 5/12 column and right column size Twelfths - 7/12 column and how can I render this logically in template left zie col-md-5 and right column size col-md-7 I want more than this one www.accordbox.com/blog/how-use-streamfield-wagtail/
@paolo-e-basta
@paolo-e-basta 5 жыл бұрын
@@tanveerahmed1040 I found this github.com/squareweave/wagtailcolumnblocks. IIRC somewhat worked
@unperrier5998
@unperrier5998 4 жыл бұрын
FYI "return None" is the default, it doesn't need to be written, it will just happen.
@CodingForEverybody
@CodingForEverybody 4 жыл бұрын
Yes you're correct. But to explicitly state something isn't harmful. We often see the .get() method being used with .get("something", None) and None is also the default if nothing is returned - also good to know but good to return so everyone who doesnt know will know, you know?
@wawwaw7063
@wawwaw7063 4 жыл бұрын
for the latest_posts in block.py, I tried to add 'from blog import models' but then it says AttributeError: module 'streams.blocks' has no attribute 'TitleAndTextBlock' , it seems I cannot import the BlogDetailPage
@unperrier5998
@unperrier5998 4 жыл бұрын
The performance advantage doesn't hold with Jinja2 because jinja compiles the templates into python bytecode and caches it.
@CodingForEverybody
@CodingForEverybody 4 жыл бұрын
Good to know! Tbh I dont use Jinja at all. Probably never will :)
@excellencemichel4875
@excellencemichel4875 4 жыл бұрын
Hi ! Thank you for this amazing work. I wonder, there are any way to use wagtail reverse url system in this case in LinkStructValue class to return absolute url if the url is internal url (I mean internal page url) because when I use your method I got Error 404 because the url go to /blog/ (If I choose blog page for url page ). Thank you !
@CodingForEverybody
@CodingForEverybody 4 жыл бұрын
StreamField's belong on pages (most of the time) and a page will always have a URL you can reverse. When using a PageChooserBlock you can use some custom StreamField logic. This video might be helpful: learnwagtail.com/tutorials/adding-custom-streamfield-logic/
@excellencemichel4875
@excellencemichel4875 4 жыл бұрын
@@CodingForEverybody Thank you so much for your answer! If you don't mind I have a question about wagtail hook for richtext formatting. Is it possible to do a formatting that allows for example to put an image on the left and put text right in the same line?
@CodingForEverybody
@CodingForEverybody 4 жыл бұрын
@@excellencemichel4875 Yep, that comes default with Wagtails RichText editor (called Draftail)!
Wagtail CMS: Registering Snippets using Django Models
13:35
Coding For Everybody
Рет қаралды 10 М.
Creating a Call to Action StreamField in Wagtail CMS using a StructBlock
18:53
Coding For Everybody
Рет қаралды 10 М.
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 39 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
00:56
صدام العزي
Рет қаралды 57 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 59 МЛН
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 64 МЛН
How to Use ListBlocks in Wagtail CMS to Create Repeating StreamField Content
19:52
How To Write Unit Tests in Python • Pytest Tutorial
35:34
pixegami
Рет қаралды 134 М.
How to Add a Basic StreamField to your Wagtail CMS Page
19:34
Coding For Everybody
Рет қаралды 26 М.
How to Preprocess Images for Text OCR in Python (OCR in Python Tutorials 02.02)
53:24
Python Tutorials for Digital Humanities
Рет қаралды 150 М.
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 39 МЛН