代码之家  ›  专栏  ›  技术社区  ›  Anmol Dua

使用django身份验证重置密码

  •  1
  • Anmol Dua  · 技术社区  · 6 年前

    当我尝试使用Django的内置密码重置系统时。它不考虑我的模板。此外,当我单击重置密码按钮时,它会将我带到Django admin重置页面( photo ).

    我的 the file map

    我的URL。py:-

    from django.conf.urls import url
    from django.contrib.auth import views as auth_views
    from . import views
    
    app_name = 'accounts'
    
    urlpatterns = [
        url(r"^login/$", auth_views.LoginView.as_view(template_name = "accounts/login.html"),name='login'),
        url(r"^logout/$", auth_views.LogoutView.as_view(template_name='thanks.html'), name="logout"),
        url(r"^signup/$", views.SignUpView, name="signup"),
        url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',views.activate, name='activate'),
        url(r'^password_reset/$', auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'), name='password_reset'),
        url(r'^password_reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
        url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
        url(r'^reset/done/$', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    ]
    

    模板:-

    password\u reset\u表单。html

      {% extends 'reg_base.html' %}
    {% load bootstrap3 %}
    
    {% block body_block %}
      <h3 class="display-1">Forgot password</h3>
      <form method="POST">
        {% csrf_token %}
        {% bootstrap_form form %}
        <button class="btn btn-primary" type="submit">Submit</button>
      </form>
    {% endblock %}
    

    密码\u重置\u确认。html

    {% extends 'reg_base.html' %}
    {% load bootstrap3 %}
    {% block body_block %}
      {% if validlink %}
        <h3 class="display-2 text-center">Change password</h3>
        <form class="form-control" method="post">
          {% csrf_token %}
          {% bootstrap_form form %}
          <button class="btn btn-default" type="submit">Change password</button>
        </form>
      {% else %}
        <p class="lead">
          The password reset link was invalid, possibly because it has already been used.
          Please request a new password reset.
        </p>
      {% endif %}
    {% endblock %}
    

    已安装的应用程序

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'bootstrap3',
        'accounts',
    ]
    

    主URL。py公司

    from django.conf.urls import url, include
    from django.contrib import admin
    from . import views
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$',views.HomepageView.as_view(),name='home'),
        url(r'^accounts/', include("accounts.urls")),
        url(r'^accounts/', include("django.contrib.auth.urls")),
    ]
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Alasdair    6 年前

    你有 django.contrib.admin 在上面 accounts 在您的 INSTALLED_APPS . 这意味着 registration/password_reset_form.html 从…起 django。承包商。管理 将首先找到并使用,而不是中的模板 账户 .

    你可以移动 账户 在上面 django。承包商。管理 因此Django首先搜索该应用程序,或者将模板名称重命名为不冲突的名称(例如。 registration/my_password_reset_form.html )并使用新模板名称更新URL模式。