python - 试图覆盖在 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_login
和account_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 出现时,会发生两件事:
- home.html 上的链接仍然指向 allauth 链接
- Landing/Home 指向自定义模板,但仍路由到 allauth 页面。
如何设置视图、链接和路由到正确的页面?
谢谢!
解决方案
在Landing/urls.py
,login
和signup
仍然指向 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')
推荐阅读
- microservices - 微服务 - 如何使用 JWT 对单独的 API 微服务进行身份验证
- regex - 为什么这个https?和 (|en) 正则表达式不起作用?
- javascript - 如何在 Mongoose 中找到与数组字段的最后一个元素与给定查询值匹配的所有文档?
- php - 如何按属性 LARAVEL 对全局范围进行排序
- javascript - Vee-validate 验证禁用字段
- string - Boot.Locale 的“线性/连续文本块”是什么意思?
- dax - DAX 查询 countdistinct
- python - numpe.zeros 错误 0 正在被转换
- python - 使用 numpy 实现微分矩阵时出错
- html - Windows 上的水平正文溢出,但不是 macOS