Home > Enterprise >  How to switch wagtail homepage depending on user logged in status
How to switch wagtail homepage depending on user logged in status

Time:03-04

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 >
    {% for post in page.blogs %}
    {% with post=post.specific %}

    <div >
        <div >
            <div >
                <strong >{{ post.category }}</strong>
                <div >{{ post.date }}</div>
                <h3 >{{ post.title }}</h3>
                <p>{{post.intro}}</p>
            </div>
            
            <div >
                <a href="{% pageurl post %}" >
                {% 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?

CodePudding user response:

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