问题
I've been trying to get django-allauth working for a couple days now and I finally found out what was going on.
Instead of loading the base.html
template that installs with django-allauth, the app loads the base.html
file that I use for the rest of my website.
How do i tell django-allauth to use the base.html template in the virtualenv/lib/python2.7/sitepackages/django-allauth
directory instead of my project/template
directory?
回答1:
Unless called directly, your base.html
is an extension of the templates that you define.
For example, if you render a template called Page.html
- at the top you will have {% extends "base.html" %}
.
When defined as above, base.html
is located in the path that you defined in your settings.py
under TEMPLATE_DIRS = ()
- which, from your description, is defined as project/template
.
Your best bet is to copy the django-allauth base.html
file to the defined TEMPLATE_DIRS
location, rename it to allauthbase.html
, then extend your templates to include it instead of your default base via {% extends "allauthbase.html" %}
.
Alternatively you could add a subfolder to your template location like project/template/allauth
, place the allauth base.html
there, and then use {% extends "allauth/base.html" %}
.
回答2:
Two years later this continues to be a problem and the accepted answer is missing some new information.
On github I discovered that all allauth templates derive from account/base.html, which derives from base.html. My solution was:
- In
virtualenv/lib/python2.7/sitepackages/django-allauth/templates
, copy the entire contents ofbase.html
to replace everything inaccount/base.html
(i.e. replace the{% extends 'base.html' %}
statement) - Delete allauth's
base.html
. It is now redundant.
Done!
回答3:
All of the answers provided force you to rewrite files and modify your own project to fit in with allauth, which is a completely unacceptable workflow. Such a third-party application should not have such manipulative power over your own project.
Truly, the easiest way to handle this situation, especially based on the answers provided, is to simply move the allauth app and its associated apps to the end of your INSTALLED_APPS
list in your settings.py
file. Django will find your base.html
template before it finds the other base.html
template in the other apps listed beneath it.
Problem solved.
回答4:
django-allauth
templates seem to extend account/base.html
(example), which extends base.html
.
So copy base.html to for example myapp/templates/account/base.html
, and make sure that myapp
loads before django-allauth
(by putting it higher in INSTALLED_APPS
).
Now the django auth templates will extend your account/base.html
, which will be django-allauth
's base.html
.
It's not perfect, since django-allauth
's base.html
might update and you'll miss those updates. But it seems better than renaming all your imports or changing django-allauth
code.
Note that you can also just put a {% block content %}
in your base.html
around the content, and then django-allauth
will use that style, which seems like a good thing in many cases.
回答5:
Allauth
tries to extend myproject/templates/base.html
. The easiest ways are to move base.html
to myproject/templates/site/
in order to get myproject/templates/site/base.html
or simply rename base.html
回答6:
I had the opposite problem: I was trying to use my own base.html
file, but my Django project was grabbing the django-allauth
version of base.html
. It turns out that the order you define INSTALLED_APPS
in settings.py
affects how templates are rendered. In order to have my base.html
render instead of the one defined in django-allauth
, I needed to define INSTALLED_APPS
as the following:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# custom
'common',
'users',
'app',
# allauth
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
来源:https://stackoverflow.com/questions/16526314/django-allauth-loads-wrong-base-html-template