How to switch wagtail homepage depending on user logged in status

Viewed 66

I have previously been using path("", include(wagtail_urls)) for my home_page url which displays the template at home/home_page.html correctly

I wish to however display different pages depending on whether a user is logged in or not so have changed this to:

def logged_in_switch_view(logged_in_view, logged_out_view):
    '''switches views dependedn on logon status'''
    def inner_view(request, *args, **kwargs):
        if request.user.is_authenticated:
            return logged_in_view(request, *args, **kwargs)
        return logged_out_view(request, *args, **kwargs)

    return inner_view 

urlpatterns = [

    path("", 
            logged_in_switch_view(
            TemplateView.as_view(template_name="home/home_page.html")),
            TemplateView.as_view(template_name="home/not_authenticated.html")),
            name="home"),
]

With this approach (directly specifying the template rather than using wagtail_urls) the home page does not display correctly when logged in, in that all the wagtail tags in the html e.g. the blog posts are not displaying

home_page.html

{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block content %}
<main class="container">
    {% for post in page.blogs %}
    {% with post=post.specific %}

    <div class="col-md-8 mx-auto px-auto">
        <div class="row border rounded overflow-auto flex-md-row mb-4 shadow-sm position-relative ">
            <div class="col p-4 d-flex flex-column position-static">
                <strong class="d-inline-block mb-2 text-primary">{{ post.category }}</strong>
                <div class="mb-1 text-muted">{{ post.date }}</div>
                <h3 class="mb-0">{{ post.title }}</h3>
                <p>{{post.intro}}</p>
            </div>
            
            <div class="col-auto my-auto py-2 px-2 d-none d-lg-block">
                <a href="{% pageurl post %}" class="stretched-link">
                {% with post.main_image as main_image %}{% if main_image %}
                {% image main_image min-250x250  max-350x350%}
                {% endif %}{% endwith %}
                </a>
            </div>
        </div>
        </div>
    {% endwith %}
    {% endfor %}
        
    </main>

{% endblock %}

How should I specify the home_page in the logged_in_switch_view function?

1 Answers

The include(wagtail_urls) pulls in Wagtail's page handling logic for selecting which page should be returned for a given URL. If you swap that line out with your own code, you're effectively swapping out all of Wagtail...

First, consider whether you're reinventing the page privacy feature that Wagtail already provides. If you want the site as a whole to require a login, and non-logged-in users to be given a blanket generic login form, you can enable this using the Privacy control under the Settings tab (or in the top right corner on older Wagtail releases) when editing the homepage, and set WAGTAIL_FRONTEND_LOGIN_TEMPLATE = "home/not_authenticated.html" in your project settings file.

If you just want to swap the template for the homepage specifically, you can do that by defining a get_template method on your HomePage model:

class HomePage(Page):
    # ...
    def get_template(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return "home/home_page.html"
        else:
            return "home/not_authenticated.html"

This way, your homepage will select one template or the other, but still keep the page object available on the template.

Related