How to edit django-allauth default templates?

梦想的初衷 提交于 2019-12-18 15:09:30

问题


i'm using Django 1.10 and i want to add the allauth's app for login, signin, etc, to my website. I've installed allauth from pip, and tried to put the templates from allauth repository inside my templates folder and call them but i don't know how to make it work.


回答1:


The correct answer can be found here: https://stackoverflow.com/a/31282443/4992248

  1. Create yourproject/templates/allauth/account/ and paste here all templates you need to edit from /myproject/Lib/site-packages/allauth/templates/account.

If you need to make changes for socialaccount templates, create also yourproject/templates/allauth/socialaccount/

  1. Edit 'DIRS' in settings.py like 'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'allauth')],

At the end it should look somethink like this:

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',
            ],
        },
    },
]
  1. You never should do any code changes at /Lib/site-packages/*, because all changes are lost once a package is updated.



回答2:


It seems that the documentation of the module is out of date. For Django 1.10 you should do the following:

  • download the module with pip
  • add the following to INSTALLED_APPS(/settings.py file)

'django.contrib.sites', # first place
'allauth',  # after your modules declarations
'allauth.account',
'allauth.socialaccount',
  • add the backends declarations and another stuff needed by allauth
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
  • seems that for django 1.10 is not needed to modify TEMPLATES section (django-allauth==0.28.0). You can verify the modules versions using the "pip freeze" command.

  • create an artificial module to override the templates; for example, my project is named irj_app and I've add a new application called _shared, then i have the following structure, and add it to INSTALLED_APPS before 'allauth' declarations :

irj_app / _shared

  • i've created a templates directory inside "_shared" folder and i've added a file called "base.html" that overrides the allauth template. what i'd found is that django-allauth creates a template that overrides the layout that you've made before, then you need to intercept the django-allauth templates to change this behavior. Also you can override any template of this authentication mechanism. For example i have:

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)

hope it helps




回答3:


Try This:

Create account directory in your app's template dir so that it looks like below

yourppname/templates/account

and files

yourppname/templates/account/login.html

yourppname/templates/account/signup.html

and add below to your TEMPLATE DIRS Remember to change yourappname to your app's name

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',
        ],
    },
},
]



回答4:


This worked for me using Django 2.1.7 and django-allauth 0.39.1:

  • In the folder yourapp/templates/ create a folder named account so at the end the structure is yourapp/templates/account/ and add all templates that you want to override like login.html or signup.html.
  • In settings.py my Template Dirs remain the same

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




回答5:


Allauth templates can be overridden just like the normal template overriding methods.

  1. Set template directory

TEMPLATE_DIRS = ( os.path.join(BASE_DIR,'templates'), os.path.join(BASE_DIR,'templates'))

  1. Your template directory will be in project directory. Go inside your template directory and create a directory named allauth, inside allauth create a template directory and inside that create a directory accounts

  2. Create html files with same name as allauth templates. Refer to allauth github repository for more info on template names.




回答6:


In django-allauth==0.36.0

  • let's say you wanna customize the login page.
  • don't need to change TEMPLATES setting
  • just create a folder named account in your project templates folder then:
  • clone the project git clone https://github.com/pennersr/django-allauth cd django-allauth/allauth/templates/account
  • Copy base.html and login.html to the created account folder
  • I tried, it works.

Official Link




回答7:


Well, i was just able to do it.

I didn't know where these templates were but i found that, in my case (i'm using virtual env):

Envs/myproject/Lib/site-packages/allauth/templates

i modified the base.html and added my static folder with all my bootstrap stuff and jquery to the settings in the file:

app_settings.py

and added this.

...
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

And that's all.

I don't know if this is the propper way to do it but if someone have a better answer please post it.



来源:https://stackoverflow.com/questions/39009638/how-to-edit-django-allauth-default-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!