python - 试图覆盖在 python 3.9 中识别的 Django 3.2 应用程序上的 allauth 模板

标签 python django

我在 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',
]
研究网址 #该项目
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')),
]
登陆/urls.py #应用级网址
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都在登陆网址和登陆 View 中
登陆意见
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'

我的树
enter image description here
我可以导航到 localhost:8000,当 html 出现时,会发生两件事:
  • home.html 上的链接仍然指向 allauth 链接
  • Landing/Home 指向自定义模板,但它仍然路由到 allauth 页面。

  • 如何设置 View 、链接和路由到正确的页面?
    谢谢!

    最佳答案

    Landing/urls.py , loginsignup仍然指向 allauth View ( allauth.account.views.LoginViewallauth.account.views.SignupView )而不是被覆盖的 View 。
    您可以尝试从以下位置更改它们:

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

    关于python - 试图覆盖在 python 3.9 中识别的 Django 3.2 应用程序上的 allauth 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68311113/

    相关文章:

    python - 在 python 中使用 'finally' 子句的范围是什么?

    python - 创建 flask-SQLAlchemy BaseQuery 对象的两种方法 - 只有一种有效,为什么?

    python - 将背景图像添加到 3d 图

    python - Django - 以 latin1 格式发布数据,解码为 utf-8

    python - '编程错误 : function avg(character varying) does not exist' - Django project

    python - 没有外键错误的django中间模型

    python - psycopg2.Error 没有被 python try/except block 捕获

    python - Django 通用 View 中 get_context_data 和 queryset 之间的区别?

    Python稀疏矩阵删除除一个之外的重复索引?

    python - 没有这样的文件或目录错误