wagtailwagtail-admin

Change the content in the main page depending on whether the user is logged in or not


I found out how to edit the homepage and add HTML contents. But I want the content to depend on whether the user is logged in or not (e.g., if the user is not logged in, write "please log in"; otherwise, write "hello user"). I tried to write the following code, which I found somewhere on the net, in the "html" text-box in the editor:

{% if user.is_authenticated %}To proceed, please log in with an account that has access.{% endif %}

But when I published it, I just saw this exact text at the bottom of the page (below the image), so probably it is not supposed to work.

How can I do this through the wagtail editor?


Solution

  • You cannot insert Django template code into content fields, by design - the whole point of a content management system is to keep editorial content separated from code.

    You can include the above code in the page's template, although the correct code would be {% if request.user.is_authenticated %}. If you want the text to be editable within the Wagtail admin interface, you can define a separate field for it (named logged_in_message, for example) and output that field on the template:

    {% if request.user.is_authenticated %}
        {{ page.logged_in_message|richtext }}
    {% endif %}