【问题标题】:How to render django-allauth templates如何渲染 django-allauth 模板
【发布时间】:2017-10-13 05:55:01
【问题描述】:

我不明白为什么我的 django-allauth 模板无法呈现。我将 allauth 目录复制到我的项目中。我还有一个以引导程序为主题的目录应用程序,应该在登录后显示。我希望显示来自 django-allauth 的注册和登录表单,登录后随后重定向到呈现引导主题的目录应用程序模板。

在 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 目录设置为:

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 = '/'

将myproject中的url路由设置为:

urlpatterns = [
        url(r'^accounts/', include('allauth.urls')),             
    ]

如上所述,我的问题是为什么没有渲染 django-allauth 模板?

【问题讨论】:

  • 你能显示错误吗?
  • @AstikAnand 没有“错误”消息,除了显示的模板应该是来自 django-allauth 的登录和注册表单,而不是源自目录应用程序的项目(引导)模板。
  • 也许模板名称冲突尝试更改项目的模板名称。

标签: django twitter-bootstrap templates django-allauth


【解决方案1】:

您缺少一些东西: 在 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 找到模板。但是您需要创建一个帐户文件夹并将它们放在其中以覆盖默认文件夹。

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 1970-01-01
    • 2020-10-21
    • 2018-05-20
    • 2018-11-05
    • 2016-08-25
    • 2016-10-31
    • 2021-11-25
    • 2013-10-10
    相关资源
    最近更新 更多