Home Ask Login Register

Developers Planet

Your answer is one click away!

massive_dynamic February 2016

Wagail one page with different content

I am new to Wagtail and python, so could apprectiate some help with my problem.

I have a web app (wagtail web site + rest api backend).

On my website I have 2 pages:

  • HomePage with list of accessible objects (e.g. photos)
  • PhotoPage with a detailed information on photo

What I want to achive:

  • When I click on photo on homepage I am redirected to the photopage
  • I fill the photopage with information I got from backend
  • And the photopage url is smth like this http://example.com/photo?id=12345

So, I want to

  • have 1 model for photopage
  • fill photopage based on a requested url (i.e. from homepage I redirect user to example.com/photo?id=12345 and it is filled with information on photo with id=12345)

I guess there should be some middleware to parse requested url to get the id and fill the page with info from API. Is there any standard solution to this issue?


Ian Price February 2016

Page objects (and any classes that inherit from Page) have a get_context method that can be used to add context pre-rendering of your templates.

from django.shortcuts import get_object_or_404

class PhotoPage(Page):
    # your model definition ...
    def get_context(self, request):
        context = super(PhotoPage, self).get_context(request)
        photo_pk = request.GET.get('id',None)
        photo = get_object_or_404(YourPhotoModel,pk=photo_pk) # if no matching photo, return 404. You can do whatever you like instead :)
        context['photo'] = photo
        return context

Now in your photo template you can access your Photo model instance directly...

{{ photo.some_attribute }}
{{ photo.some_other_attribute }}

Post Status

Asked in February 2016
Viewed 2,892 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life