首页 > 解决方案 > 试图覆盖在 python 3.9 中识别的 Django 3.2 应用程序上的 allauth 模板

问题描述

我在 Django 3.2 上与 ALLAUTH 合作。我发现的大多数解决方案都是针对 Django 1 的,所以我希望在这里找到更新的东西。我已经安装了模块/应用程序,但是在用我自己的模板覆盖模板时遇到了问题。

在 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',
]

研究网址#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级 url

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')
]

主页.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>
<...>

注意:account_loginaccount_signup都在登陆网址和登陆视图中

着陆视图

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'

我的树

在此处输入图像描述

我可以导航到 localhost:8000,当 html 出现时,会发生两件事:

  1. home.html 上的链接仍然指向 allauth 链接
  2. Landing/Home 指向自定义模板,但仍路由到 allauth 页面。

如何设置视图、链接和路由到正确的页面?

谢谢!

标签: pythondjango

解决方案


Landing/urls.py,loginsignup仍然指向 allauth 视图 ( allauth.account.views.LoginView, allauth.account.views.SignupView) 而不是被覆盖的视图。

您可以尝试将它们从以下位置更改:

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

到:

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

推荐阅读