如何修改django-allauth的默认模板?

Seb*_* B. 10 django django-templates django-allauth

我正在使用Django 1.10,我想将allauth的应用程序添加到我的网站登录,登录等.我从pip安装了allauth,并试图将allauth存储库中的模板放在我的模板文件夹中并调用它们,但我不知道如何使其工作.

Tit*_*ter 20

可在此处找到正确答案:https://stackoverflow.com/a/31282443/4992248

  1. yourproject/templates/allauth/account/在此处创建并粘贴您需要编辑的所有模板/myproject/Lib/site-packages/allauth/templates/account.

如果您需要对socialaccount模板进行更改,请同时创建yourproject/templates/allauth/socialaccount/

  1. 编辑'DIRS'settings.py'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'allauth')],

最后它应该看起来像这样:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'allauth')],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': False,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.template.context_processors.media',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)
  1. 您永远不应该进行任何代码更改/Lib/site-packages/*,因为更新包后所有更改都将丢失.


and*_*hdo 6

似乎该模块的文档已过时.对于Django 1.10,您应该执行以下操作:

  • 用pip下载模块
  • 将以下内容添加到INSTALLED_APPS(/settings.py文件)

'django.contrib.sites', # first place
'allauth',  # after your modules declarations
'allauth.account',
'allauth.socialaccount',
Run Code Online (Sandbox Code Playgroud)
  • 添加后端声明和allauth所需的其他内容
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
Run Code Online (Sandbox Code Playgroud)
  • 似乎对于django 1.10不需要修改TEMPLATES部分(django-allauth == 0.28.0).您可以使用"pip freeze"命令验证模块版本.

  • 创建一个人工模块来覆盖模板; 例如,我的项目名为irj_app,我添加了一个名为_shared的新应用程序,然后我有以下结构,并在'allauth'声明之前将其添加到INSTALLED_APPS:

irj_app / _shared

  • 我在"_shared"文件夹中创建了一个模板目录,并添加了一个名为"base.html"的文件,该文件覆盖了allauth模板.我发现django-allauth创建了一个模板,它覆盖了你以前做过的布局,然后你需要拦截django-allauth模板来改变这种行为.您也可以覆盖此身份验证机制的任何模板.例如我有:

irj_app / _shared / templates / base.html

irj_app / _shared / templates / account / base.html

irj_app / _shared / templates / account / signup.html

irj_app / _shared / templates / _shared / adminlte-template / ... (template for other modules)

希望能帮助到你


utr*_*tri 5

尝试这个:

在应用程序的模板目录中创建帐户目录,如下所示

yourppname/模板/帐户

和文件

yourppname/templates/account/login.html

yourppname/templates/account/signup.html

并将下面添加到您的模板目录记住将您的应用名称更改为您的应用名称

os.path.join(BASE_DIR, 'yourappname', 'templates')

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'yourappname', 'templates')],
    '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',
        ],
    },
},
]
Run Code Online (Sandbox Code Playgroud)


小智 5

这对我使用 Django 2.1.7 和 django-allauth 0.39.1 有效:

  • 在文件夹中yourapp/templates/创建一个名为accountso的文件夹,结构是yourapp/templates/account/并添加所有要覆盖的模板,如login.htmlsignup.html

  • settings.py我的模板目录中保持不变

    'DIRS': [os.path.join(BASE_DIR, 'templates')],