首页 > 解决方案 > 显示 django admin 而不是我的 html

问题描述

我正在尝试在 Django 中更改密码。问题是它使用 Django admin 而不是我的 html 模板。如何让我的模板过去,而不是管理员?

urlpatterns = [
    path('', index, name='index'),
    path('registration/', registration, name='registration'),
    path('accounts/', include('django.contrib.auth.urls'), name='login'),
    path('accounts/', include('django.contrib.auth.urls'), name='logout'),
    path('accounts/password_reset/', PasswordChangeView.as_view(template_name="password_change.html"), name='password_reset'),
]

在此处输入图像描述

标签: htmldjango

解决方案


Django 使用第一个匹配的 url 模式。由于您声明了自己的 url 模式来替换其中的一个,django.contrib.auth.urls因此您应该将其写在这些 url之上。

您还为包含的网址命名,这没有意义。此外,您要替换的模式password_reset应该使用PasswordResetView(您编写PasswordChangeView),我假设您实际上想要替换password_change

也无需两次包含相同的 url 模式(django.contrib.auth.urls无论如何,当您编写一次时,将包含所有 url)。

urlpatterns = [
    path('', index, name='index'),
    path('registration/', registration, name='registration'),
    path('accounts/password_change/', PasswordChangeView.as_view(template_name="password_change.html"), name='password_change'),
    path('accounts/', include('django.contrib.auth.urls')),
]

推荐阅读