overriding default templates of django-allauth
Asked Answered
R

7

30

I used this social registration/signup library django allauth for a project of mine. How do i customize the default templates and forms to give a better look and feel?

Resort answered 24/2, 2012 at 20:26 Comment(1)
Check this detailed and easy to follow answer https://mcmap.net/q/472579/-how-to-override-template-in-django-allauthFimbriation
M
12

the latest version of all-auth on github has its templates outside, however the one on Pypi is not, all you need to do is clone the repo in your project directory and override the templates. As simple as that.

Mortgagor answered 5/3, 2012 at 12:19 Comment(3)
Cloning repository is considered a very bad practice.Fahey
@Fahey Could you elaborate?Nolan
He probably means that cloning a repo locally means it's more difficult to stay up-to-speed with further updates to all-auth.Cornflakes
F
22

Assuming you have set a project level templates directory using the TEMPLATE_DIRS setting like:

TEMPLATE_DIRS = [
    os.path.join(PROJECT_DIR, 'templates'),
]

You should be able to copy all of the folders shown here into that directory and edit them as you need. Most of the templates seem to be filling a {% block content %} block, so it's probably easiest if your site_base.html template defines that block somewhere.

If you haven't set TEMPLATE_DIRS, you can do the same thing, but copy the template folders into the templates directory of one of your apps. I prefer to set TEMPLATE_DIRS and keep the main site templates like base.html there, since they don't really belong to a particular app, but that's really just a preference; the template loader should find them either way.

Frigidaire answered 5/3, 2012 at 17:26 Comment(2)
In the current version it seems you should have a base.html (still with {% block content%}).Nacelle
TEMPLATE_DIRS is deprecated and part of TEMPLATES docs.djangoproject.com/en/1.8/ref/templates/upgrading. Copying templates/anything into package is bad practice and will hurt you later.Fog
A
13

In your views:

from allauth.account.views import SignupView, LoginView


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


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

Do pay attention to the examples and docs for structuring your own templates.

Watch this piece in the example templates though:

<form id="signup_form" method="post" action="{% url 'account_signup' %}">

I had to remove the URL link to make it work properly in my own template:

<form id="signup_form" method="post" action="">'
Assentor answered 6/4, 2016 at 15:2 Comment(2)
Be sure you are pointing your URLs to these replacement views.Assentor
This is the most simple solution that I had on allauth overriding template.Stature
M
12

the latest version of all-auth on github has its templates outside, however the one on Pypi is not, all you need to do is clone the repo in your project directory and override the templates. As simple as that.

Mortgagor answered 5/3, 2012 at 12:19 Comment(3)
Cloning repository is considered a very bad practice.Fahey
@Fahey Could you elaborate?Nolan
He probably means that cloning a repo locally means it's more difficult to stay up-to-speed with further updates to all-auth.Cornflakes
H
9

Use the same logic as overriding admin templates.

Hypertrophy answered 6/3, 2012 at 9:29 Comment(1)
Whether the link is to 1.9 or 1.10 docs, it's amazing to read "Within this admin directory, create sub-directories named after your app." Name "directories", plural, after your "app", singular?Malone
M
4

Have a look at the example application; it has a templates folder that indicates the layout of the necessary templates

Mandibular answered 24/2, 2012 at 20:37 Comment(2)
well what i have done to implement the library is added a site_base.html in my templates folder with two blocks <!doctype html> <head> <title> {% block head_title %} {% endblock %} </title> </head> <body> <!-- done for django-allauth --> {% block body %}{% endblock %} </body> </html> how do i edit or override the builtin templates, in which directory can i find them inside my virtualenv?Resort
~/.virtualenvs/yourproject/lib/python2.7/site-packages/allauthWarhol
V
4

All of these are good suggestions.

Most of these ought to work.

The only thing that worked for me, though, was to include my own templates in "project/app/templates/account/" and make sure that "django-allauth" gets listed in INSTALLED_APPS after my own app.

I discovered this here.

Villus answered 13/10, 2017 at 7:47 Comment(0)
C
2

To customize django-allauth after installing it, copy it from site-packages and paste it in your project apps directory. In this way the default allauth app and its templates being used will be those in your project's allauth app. Then if you want to modify signup.html of socialaccount then go to apps\allauth\templates\socialaccount\signup.html and modify it by editting inside 'block content' tag:

{% block content %}
    // your customized html for signup form
{% endblock %}

Hope this will help you.

Cupp answered 5/3, 2012 at 11:26 Comment(3)
I think it should work. Because first django looks templates in inside app (apps\my_app\templates\my_app), then in project templates directory (templates\my_app) and then goes to your environments site-packages. Have you tried to change the html in site-packages allauth templates?Cupp
I do not think that copying is a good way. Much better is to git clone into project folder rather than duplicating code.Warhol
Copying the templates would be preferred I'm my case, as I'd rather just copy the templates over than clone the repo with all he code I don't need to modifyAnastos

© 2022 - 2024 — McMap. All rights reserved.