Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

python - How to get the values from a Wagtail CMS Stream field panel using Django Models?

I'm building a webpage using wagtail CMS, Django, Postgresql and at the bottom of the page, I'm building a section where I would display the videos using pagination.

I'm trying to retrieve the data from Django Models using all_posts = MultiBlogPage.objects.values("all_blogs_content_pages")

and I'm getting the output as

<PageQuerySet [{'all_blogs_content_pages': 
[
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ee2d21d0>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ed8fa2e8>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35f617e6a0>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ed90aac8>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ed90af60>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ed90a978>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ed90ae48>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ee2b9320>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ee2b9630>,
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ee339f28>, 
<wagtail.core.blocks.stream_block.StreamValue.StreamChild object at 0x7f35ee339470>
]}]>

Could someone please look at my code below and let me know how to get the exact values from Django Models ?

def get_context(self, request, *args, **kwargs):
    context = super(MultiBlogPage, self).get_context(request, *args, **kwargs)
    context['multiblog_page'] = self
    
    // The name of the stream field panel is "all_blogs_content_pages"
    all_posts = MultiBlogPage.objects.values("all_blogs_content_pages")

    print("all_posts...",all_posts)

    paginator = Paginator(all_posts, 3)
    print("paginator", paginator)
    page = request.GET.get("page")

    try:
        posts = paginator.page(page)        
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    
    context["posts"] = posts
    return context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wagtail models can be used like standard django models, so converting the models to a list of dicts does not seem necessary.

live(), filters only published versions, and specific() gets the actual model from your models.py rather than the base Page class.

def get_context(self, request, *args, **kwargs):
    context = super(MultiBlogPage, self).get_context(request, *args, **kwargs)
    context['multiblog_page'] = self
    
    // The name of the stream field panel is "all_blogs_content_pages"
    all_posts = MultiBlogPage.objects.live().specific()

    print("all_posts...",all_posts)

    paginator = Paginator(all_posts, 3)
    print("paginator", paginator)
    page = request.GET.get("page")

    try:
        posts = paginator.page(page)        
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    
    context["posts"] = posts
    return context

template.html

<li>
  {{ post.all_blogs_content_pages }}
</li>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...