Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override template in django-allauth?

Answers in other questions leave impression that this is in fact very easy:

  • django-allauth configuration doubts
  • overriding default templates of django-allauth

However, I can't get it to work at all.

From example app settings I can see that django-allauth supposedly expects it's templates to be in account, openid and socialaccount directories. But when I put template at TEMPLATE_DIR/account/signup.html it doesn't get loaded, signup view displays template bundled with django-allauth. What do I miss?

like image 340
Red Avatar asked Sep 13 '13 16:09

Red


3 Answers

I eventually resorted to loading my app before django-allauth. In settings.py:

INSTALLED_APPS = (
    ...
    'myapp',
    'allauth',
    'allauth.account'
)

This solution goes against what's presented in example app, but I was not able to solve it in other way.

like image 123
Red Avatar answered Oct 23 '22 11:10

Red


To this day--- we're now on Django-1.10.5--- the django-allauth docs remain most unhelpful on this score. It does seem to be that Django looks in the templates directory of the first app listed, the setting of DIRS in TEMPLATES in settings.py notwithstanding. I'm providing an answer only to help you implement Adam Starrh's answer, to help with the reverse urls (I got errors until I took care of those).

In your urls.py file put:

from allauth.account.views import SignupView, LoginView, PasswordResetView

class MySignupView(SignupView):
    template_name = 'signup.html'

class MyLoginView(LoginView):
    template_name = 'login.html'

class MyPasswordResetView(PasswordResetView):
    template_name = 'password_reset.html'

urlpatterns = [
    url(r'^accounts/login', MyLoginView.as_view(), name='account_login'),
    url(r'^accounts/signup', MySignupView.as_view(), name='account_signup'),
    url(r'^accounts/password_reset', MyPasswordResetView.as_view(), name='account_reset_password'),
]

Presently the views.py file is here, so you can extend the above to other templates.

I must add that you still need in TEMPLATES, something like:

'DIRS': [
    os.path.join(PROJECT_ROOT, 'templates', 'bootstrap', 'allauth', 'account'),
],

And in this example that would be if your templates are in /templates/bootstrap/allauth/account, which they are in my case. And:

PROJECT_ROOT = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))

EDIT... THE PROPER WAY:

OK, the above works, to a point, and it's good that it directly sets the template to what you want. But as soon as you include social apps you'll start to get reverse url errors such as for dropbox_login, for which you have not provided a named view.

After reading Burhan Khalid's comment on this other stackoverflow thread that the questioner found, I eventually found that the following works:

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

And this yields /home/mike/example/example/templates/example on the development server in my case, as I am running the example app from git clone git://github.com/pennersr/django-allauth.git.

Into that dir of DIRS I copied the entire subdirectories account and socialaccount from the provided sample bootstrap templates. This is utterly contrary to the directory structure of example as it comes from github and to notes in the settings.py file of example.

And you leave urls.py just as in the example app, with simply:

    url(r'^accounts/', include('allauth.urls')),  
like image 27
Mike O'Connor Avatar answered Oct 23 '22 10:10

Mike O'Connor


Adding a template directory for allauth in template dirs will do the trick. In Django 1.8 his can be done by editing template dir settingsTEMPLATES as follows.

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

I think below code will work on other versions of django

TEMPLATE_DIRS = [
    os.path.join(BASE_DIR, 'templates'),
    os.path.join(BASE_DIR, 'templates', 'allauth'),
]
like image 7
cutteeth Avatar answered Oct 23 '22 09:10

cutteeth