[Fixed]-How to change template based on user authentication in django

1👍

If your template goes invalid, I suggest you to it at the views.py, an example:

from django.shortcuts import render, render_to_response

def homepage(request):
    template_name = 'homepage.html'
    extended_template = 'base_login.html'

    if request.user.is_authenticated():
        extended_template = 'base.html'

    return render(
        request, template_name, 
        {'extended_template': extended_template, ...}
    )

# homepage.html
{% extends extended_template %}

{% block content %}
  {% if request.user.is_authenticated %}
    Hello {{ request.user }}
  {% endif %}
{% endif %}

Note: if function of render still doesn’t work well, please try with render_to_response such as this answer: https://stackoverflow.com/a/1331183/6396981

👤binpy

Leave a comment