How to access default page models in template wagtail - django-models

So this awesome Wagtail/Django framework is nice!
I like it allot.
Still getting used to but seems straight forward,
What I do not understand tho is how can I access default page models and render them in the templates?
So Wagtail has this models that you make based on their Page class.
class SomeClass(Page):
"""
Some text
"""
intro = models.CharField(max_length=255, blank=True)
body_small = models.CharField(max_length=255, blank=True)
All good and well.
Now my page template looks like this
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block base_content %}
{{ page.intro }}
{{ page.body_small }}
{% endblock %}
Now I want to add the settings model items like Published Date.
Those are default from Wagtail, see:
What page model do I need to use?
{{ page.published_date }} //Does not work
Any suggestions?

The fields in the Settings tab are available as {{ page.go_live_at }} and {{ page.expire_at }}. However, these are only used for scheduled publishing so may not be a particularly relevant thing to output on the page - {{ page.first_published_at }} and {{ page.last_published_at }} are probably more useful. See http://docs.wagtail.io/en/stable/reference/pages/model_reference.html for more.

Related

how to pass django params in react components

i am new to react , i am building simple blog where user can post with title and body
in html its done with listview with:
{% for x in object_list %}
{% endfor %}
but in react component its not getting good result and showing error
here is my code:
models
class post(models.Model):
title = models.CharField(max_length=100)
body=models.TextField()
views
class list(ListView):
model = post
template_name = 'index.html'
post.js
function Post(){
return(
{% for x in object_list %}
{% endfor %}
)
}
in react what can i do to retreive data from model like we used to do in normal html, or show the object from model in components??

CollectionType adds 2 rows on + click sonata

I have extended sonata edit page with this:
{% extends '#SonataAdmin/CRUD/base_edit.html.twig' %}
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('select2', null, 'admin') }}
{% endblock %}
But now when I click + button nera "Product option group codes" , it adds 2 rows.
If I remove
{{ encore_entry_script_tags('select2', null, 'admin') }}
then it works ok - adds just one row on + click.
I have commented all code in selec2.js file, to make sure it is not causing something but still nothing changes.
If I remove
parent()
Then "Product option group codes" js stops working.
How to debug? Where could be the problem?
Noticed that this js was not by sonata, but in our script. And when adding
{{ encore_entry_script_tags('select2', null, 'admin') }}
it calls document ready in another JS 2nd time, which setups + sign listener. So 2 listeners are set up.
To avoid this, instead of encore function, using script tag solved the problem. Also changed file name which I include.
{% extends '#SonataAdmin/CRUD/base_edit.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('build/admin/admin-state-disaster-scenario.js', 'admin') }}"></script>
{% endblock %}

How do I generate a list of pages in the sections for a sidemenu in Wagtail?

I am a true beginner in Wagtail. How do I generate a list of pages in the sections for a sidemenu in Wagtail?
I have the following site structure, as an example:
home/
fruits/
apples/
oranges/
grapes/
vegetables/
kale/
spinach/
cabbage/
home is of HomePage type using home_page.html template , and all the subpages are of ContentPage type using content_page.html template.
I want to make a side menu for all the content pages, listing all the pages in their groups. For example, this list:
Fruits
Apples
Oranges
Grapes
should be the sidemenu for the pages fruits, apple, oranges, and grapes.
page.get_children in the template only lists out if the page has children, so, in this case just fruits and vegetables.
How would I go about making that sidemenu?
The examples in Wagtail's documentation seem to imply that I can't have just a generic content type like ContentPage to have the sort of listing that I want, is that true?
Thanks a bunch!
welcome to Wagtail!
As with most things in web development, there are a few ways you can do this. The simplest to understand when you're just starting is to do this all through the template. So in your home_page.html you could have:
{% for parent in page.get_children %}
Page title: {{ parent.title }} <br />
{% if parent.get_children.count %}
{% for child in parent.get_children %}
- Child page title: {{ child.title }}<br/>
{% endfor %}
{% endif %}
{% endfor %}
What this does is:
Loops through the child pages of HomePage (labeled as parent in this loop) and prints Page title: {title_here}
Then it'll check for child pages of each parent loop iteration and print - Child page title: {child_title}
There's a gotcha here though. This will only work on the home_page.html template. Once you go to /fruits/ it'll try to perform the same logic, but this time it'll think Fruits is the new HomePage
There are 2 options you can take from here.
You can add custom context to every page to make sure you're always passing in the HomePage and loop through that. This is the simplest method and I'll show you the code below. Or,
You can create a Menu system using a Django Model and registering the Menu class as a Wagtail Snippet. I have a video with all the source code available if you want to take a deeper dive into Wagtail (https://www.youtube.com/watch?v=Y8a9ROUUJXU)
To add HomePage to every ContentPage you can add it to the context of every page, like so:
class ContentPage(Page):
# Fields here
def get_context(self, request, *args, **kwargs):
"""Adding HomePage to your page context."""
context = super().get_context(request, *args, **kwargs)
context["home_page"] = HomePage.objects.first()
return context
And in your templates you'd write:
{% for child_page in home_page.get_children %}
Page title: {{ child_page.title }} <br />
{% if child_page.get_children.count %}
{% for grandchild_page in child_page.get_children %}
- Child page title: {{ grandchild_page.title }}<br/>
{% endfor %}
{% endif %}
{% endfor %}
Edit: If you're on a grandchild page, like /fruits/apples/ and want to display the parent page title, and all the sibling pages (ie. /fruits/oranges/ and /fruits/grapes/) you can loop through the sibling pages. Something like this should work:
<!-- On `/fruits/` this will be the Home Page title. On `/fruits/apples/` this will be the Fruits page title. -->
<h2>{{ self.get_parent.title }}<h2>
{% for sibling in self.get_siblings %}
{{ sibling.title }}
{% endfor %}

Photologue not showing images or thumbnails

I am not able to get photologue to show the images. What am I doing wrong?
development environment
django 2.1
python 3.5
osx, virtual_env, recent pip
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'mst/media/')
MEDIA_URL = "/media/"
urls
urlpatterns += [
...
url(r'^photologue/', include('photologue.urls', namespace='photologue')),
]
model
from photologue.models import Photo, Gallery
class PhotoExtended(models.Model):
photo = models.OneToOneField(Photo, on_delete=models.CASCADE, related_name='photo')
related_model = models.ForeignKey(MyModel, on_delete=models.CASCADE)
def __str__(self):
return self.photo.title
class GalleryExtended(models.Model):
gallery = models.OneToOneField(Gallery, on_delete=models.CASCADE, related_name='gallery')
related_model = models.ForeignKey(MyModel, on_delete=models.CASCADE)
def __str__(self):
return self.gallery.title
class based view
class MyModelList(ListView):
model = MyModel
template_name = "pictures.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['photos'] = PhotoExtended.objects.all()
context['galleries'] = GalleryExtended.objects.all()
return context
template (pictures.html):
{% block content %}
<ul>
{% for photoExtended in photos %}
<li>{{ photoExtended.photo.get_absolute_url }}</li>
<li>{{ photoExtended.photo.image }}</li>
<img src="/{{ photoExtended.photo.image }}" alt="{{ p.photo.title }}">
{% endfor %}
{% for gallery in galleries %}
<li></li>
{% endfor %}
shell response (per docs)
>>> from PIL import Image
>>>
notes
I migrated the database, and see the data in the database, and everything looks correct.
page renderings
photologue urls:
A single photo
the list of the photos
and the direct accessing of the image: (ra is the name in photologue, but em.jpeg is the file name)
and my view directly:
The template was wrong/outdated from an older version. Here is the correct usage in the template for an image within a list of images of type photologueExtended:
{% for photoExtended in photos %}
<!-- The link to the photologue template page with the photo and its gallery(s)-->
Link to page
<!-- The src link to the image thumbnail itself in the media url-->
<img src="{{ photoExtended.photo.get_thumbnail_url }}" />
<!-- The src link to the image itself in the media url-->
<img src="{{ photoExtended.photo.get_display_url }}" />
<!-- The photologue image's title/description/etc… -->
{{ photoExtended.photo.title }} <br>
{{ photoExtended.photo.description }} <br>
{% endfor %}
Also:
the url catchall in the main project urls.py was incorrect, and should be:
url(r'^$', indexView, name='indexView'),

How do I reference my site's title in a template?

I'm starting a Wagtail site. In the admin section, I went to Settings >> Sites >> localhost, and set "Site name" to "My first Wagtail site."
How do I display the site title in templates using Django code?
The tag to use is:
{{ request.site.site_name }}
Old version
<h1>Welcome to the {{ request.site.site_name }} website!</h1>
new version
{% load wagtailcore_tags %}
{% wagtail_site as current_site %}
<h1>Welcome to the {{ current_site.site_name }} website!</h1>
Reference: https://docs.wagtail.io/en/stable/releases/2.9.html#upgrade-considerations
in wagtail 4.1,use {{page.get_site}} in template.

Resources