问题
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?
回答1:
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.
回答2:
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.
'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'))
回答3:
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')),
回答4:
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'
回答5:
For me only one solution works:
first make TEMPLATE_LOADERS to load filesystem.Loader
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
second - make TEMPLATE_DIRS with path where you copied templates from allauth. Make sure you copied full folder hierarchy from templates folder of allauth app.
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'cms', 'templates', 'allauth'),
)
in this example a made path to my app, named cms because this is the main app of my project after you could start to edit base.html template
回答6:
I found a better way to do this but forgot. All Auth allows you to do this very easy but and low on documentation. Here is my next easiest solution, until I rediscover the vaster way. #lol
The code creates a custom login page but the pattern is simple and easy to replicate. You can put all this code in urls.py
, or not:
from allauth.account.views import LoginView
class Lvx(LoginView):
# Login View eXtended
# beware ordering and collisions on paths
template_name = "components/login.html"
# don't forget to create the login page, see All Auth docs
# for use. /components is in your app templates path
login = Lvx.as_view()
urlpatterns = [
url(r'^accounts/login/$', login), # usually up top
...
]
There is also a setting you can use to point to a custom page, will edit this at some point. Feedback welcome.
来源:https://stackoverflow.com/questions/18791136/how-to-override-template-in-django-allauth