如何呈现 django-allauth 模板
How to render django-allauth templates
我不明白为什么我的 django-allauth 模板无法呈现。我将 allauth 目录复制到我的项目中。我还有一个以 bootstrap 为主题的目录应用程序,应该在登录后显示。我希望显示来自 django-allauth 的注册和登录表单,并在登录后重定向到呈现 bootstrap 主题目录应用程序的模板。
在 settings.py 中,在其他必需的应用程序中,我在 INSTALLED_APPS 中的目录应用程序之后添加了 django-allauth 模块。
INSTALLED_APPS=['catalog',
'allauth',
'allauth.account',
'allauth.socialaccount',
]
添加身份验证后端为:
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"
)
在settings.py中,模板目录设置为:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'allauth/templates/account'),os.path.join(BASE_DIR,'catalog/templates/catalog')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'allauth.account.contextprocessors.account',
'allauth.socialaccount.contextprocessors.socialaccount',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'ecomstore.utils.context_processors.ecomstore',
],
},
},
]
我让 django-allauth 在登录成功时 url 路由到。添加 LOGIN_REDIRECT_URL = '/'
在我的项目中设置url路由为:
urlpatterns = [
url(r'^accounts/', include('allauth.urls')),
]
如上所述,我的问题是为什么没有呈现 django-allauth 模板?
你错过了一些东西:
在 INSTALLED_APPS:
'django.contrib.sites',
之后添加一行
SITE_ID = 1
如果您将它们放入后仍然无法正常工作,则可能是应用程序的加载顺序有问题。
Allauth 还带有自己的登录、注销和更改密码,请参阅 here
如果您要切换模板,您可以使用如下标签放入 allauth 模板:
{% url 'account_login' %} <!-- Sign In-->
如果您需要检查用户是否已登录,您可以使用 if 语句
{% if user.is_active %} <!-- checks if user signed in-->
<!-- Insert code here that appears if signed in-->
{% else%}
<!-- Insert code here to display for users that did not sign in-->
{% endif%}
最后可以找到模板 here 如果您需要设置它们的样式。但是您需要创建一个帐户文件夹并将它们放在那里以覆盖默认帐户。
我不明白为什么我的 django-allauth 模板无法呈现。我将 allauth 目录复制到我的项目中。我还有一个以 bootstrap 为主题的目录应用程序,应该在登录后显示。我希望显示来自 django-allauth 的注册和登录表单,并在登录后重定向到呈现 bootstrap 主题目录应用程序的模板。
在 settings.py 中,在其他必需的应用程序中,我在 INSTALLED_APPS 中的目录应用程序之后添加了 django-allauth 模块。
INSTALLED_APPS=['catalog',
'allauth',
'allauth.account',
'allauth.socialaccount',
]
添加身份验证后端为:
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"
)
在settings.py中,模板目录设置为:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'allauth/templates/account'),os.path.join(BASE_DIR,'catalog/templates/catalog')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'allauth.account.contextprocessors.account',
'allauth.socialaccount.contextprocessors.socialaccount',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'ecomstore.utils.context_processors.ecomstore',
],
},
},
]
我让 django-allauth 在登录成功时 url 路由到。添加 LOGIN_REDIRECT_URL = '/'
在我的项目中设置url路由为:
urlpatterns = [
url(r'^accounts/', include('allauth.urls')),
]
如上所述,我的问题是为什么没有呈现 django-allauth 模板?
你错过了一些东西: 在 INSTALLED_APPS:
'django.contrib.sites',
之后添加一行
SITE_ID = 1
如果您将它们放入后仍然无法正常工作,则可能是应用程序的加载顺序有问题。
Allauth 还带有自己的登录、注销和更改密码,请参阅 here
如果您要切换模板,您可以使用如下标签放入 allauth 模板:
{% url 'account_login' %} <!-- Sign In-->
如果您需要检查用户是否已登录,您可以使用 if 语句
{% if user.is_active %} <!-- checks if user signed in-->
<!-- Insert code here that appears if signed in-->
{% else%}
<!-- Insert code here to display for users that did not sign in-->
{% endif%}
最后可以找到模板 here 如果您需要设置它们的样式。但是您需要创建一个帐户文件夹并将它们放在那里以覆盖默认帐户。