trying to override allauth templates on a Django 3.2 app recognized in python 3.9

Viewed 167

I'm working with ALLAUTH on Django 3.2. Most solutions I found are for Django 1, so I'm hoping to find something more up-to-date here. I have the module/app installed, but I'm having problems overriding the templates with my own.

In settings.py:

INSTALLED_APPS = [
    ...
    'Landing.apps.LandingConfig',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google'
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / 'templates'
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Already defined Django-related contexts here

                # `allauth` needs this from django
                'django.template.context_processors.request',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = [
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
]

Research Urls #The Project

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('Landing.urls')),
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
]

Landing/urls.py #app-level urls

from django.contrib import admin
from django.urls import path, include

from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('login/', views.LoginView.as_view(), name='account_login'),
    path('signup/', views.SignupView.as_view(), name='account_signup')
]

Home.html

<...>
<body>
    <H1>This is the homepage</H1>
    <p><a href="{% url 'account_login' %}">login</a></p>
    <p><a href="{% url 'account_signup' %}">Create Account</a></p>

</body>
<...>

Note: account_login and account_signup are both in Landing urls and Landing Views

Landing Views

from django.shortcuts import render
from allauth.account.views import LoginView, SignupView


# Create your views here.
def home(request):
    return render(request, 'Landing/home.html')


class LandingLogin(LoginView):
    print('found Login View....')
    template_name = 'authentication/login.html'


class LandingSignup(SignupView):
    print('found Login View....')
    template_name = 'authentication/account_signup.html'

My Tree

enter image description here

I can navigate to localhost:8000, and when the html comes up, two things occur:

  1. links on home.html still point to allauth links
  2. Landing/Home points to the custom template, but it still routes to the allauth page.

How can set the view, link, and route to the correct page?

Thanks!

2 Answers

In Landing/urls.py, login and signup still points to allauth views (allauth.account.views.LoginView, allauth.account.views.SignupView) and not the overridden views.

Can you try changing them from:

    path('login/', views.LoginView.as_view(), name='account_login'),
    path('signup/', views.SignupView.as_view(), name='account_signup')

to:

    path('login/', views.LandingLogin.as_view(), name='account_login'),
    path('signup/', views.LandingSignup.as_view(), name='account_signup')

If you want to override the HTML templates that are provided by allauth, you need to save your template files in templates/account/ directory instead of templates/authentication/. Then you won't need to use the template_name variable. Also make sure that the name of the HTML file corresponds to the page you want to override. For example if you want to override the login page, you need to save your template in the templates/account/ directory with the name login.html. You can take a look at the views in the allauth library to see what name you need to keep. Take a look at the documentation for more info.

And also as the answer given by @bdbd, you would need to change the view names in the urls.py file.

Related