我在 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_login
和 account_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'
我的树 我可以导航到 localhost:8000,当 html 出现时,会发生两件事:
如何设置 View 、链接和路由到正确的页面?
谢谢!
最佳答案
在 Landing/urls.py
, login
和 signup
仍然指向 allauth View ( allauth.account.views.LoginView
, allauth.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/