如何重写Django allauth中的模板?

2024-04-27 05:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

其他问题的答案给人的印象是,这其实很简单:

但是,我一点也做不到。

example app settings我可以看到,django allauth应该希望它的模板位于accountopenidsocialaccount目录中。但是当我将模板放在TEMPLATE_DIR/account/signup.html时,它不会被加载,signup视图显示与django allauth绑定的模板。我错过了什么?


Tags: ofdjango答案模板appdefaultexampleaccount
3条回答

直到今天---我们现在是在Django-1.10.5--Django-allauth医生仍然对这一点毫无帮助。似乎Django在列出的第一个应用程序的templates目录中查找,尽管如此,在settings.py中的DIRS设置仍然有效。我提供的答案只是为了帮助您实现Adam Starrh的答案,帮助您实现反向url(在处理这些问题之前,我遇到了错误)。

在urls.py文件中输入:

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

目前views.py文件是here,因此您可以将上面的扩展到其他模板。

我必须补充一点,你仍然需要TEMPLATES,比如:

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

在这个例子中,如果你的模板在/templates/bootstrap/allauth/account中,在我的例子中就是这样。以及:

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

编辑。。。正确的方法:

好的,上面的方法是有效的,在一定程度上,它可以直接将模板设置为您想要的。但是一旦你包含了社交应用程序,你就会开始出现反向url错误,比如你没有提供命名视图的dropbox_login

在阅读了Burhan Khalid关于提问者发现的this other stackoverflow thread的评论之后,我最终发现以下几点是有效的:

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

在我的例子中,这会在开发服务器上产生/home/mike/example/example/templates/example,因为我正在从git clone git://github.com/pennersr/django-allauth.git运行example应用程序。

DIRS的目录中,我从提供的样本bootstrap模板复制了整个子目录accountsocialaccount。这与example的目录结构完全相反,因为它来自githubexamplesettings.py文件中的注释。

您只需在urls.py应用程序中留下example即可:

    url(r'^accounts/', include('allauth.urls')),  

Adding a template directory for allauth in template dirs会成功的。在Django 1.8中,可以通过如下编辑模板目录设置TEMPLATES来完成此操作。

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

我认为下面的代码可以用于django的其他版本

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

我最终还是在django allauth之前加载了我的应用程序。在settings.py中:

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

这个解决方案与示例应用程序中所提供的相反,但我无法用其他方式解决它。

相关问题 更多 >