Django Rest Framework密码Rest Confirm Email未显示表单并返回为none

flseospp  于 2023-02-20  发布在  Go
关注(0)|答案(1)|浏览(119)

在我的Django Rest框架中,用户请求重置密码,当收到电子邮件并点击链接时,url password-reset-confirm/<uidb64>/<token>/出现,但表单不显示,当我将其添加为{{form}}时,显示为NONE
当我在Django上做任何事情时,密码重置过程都运行得很好,但是如果我尝试从Django Rest Framework重置密码,表单就不会出现。
这里是主要的www.example.comurls.py

urlpatterns = [
    path('', include('django.contrib.auth.urls')),
    path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html', success_url=reverse_lazy('password_reset_done')), name='password_reset'),
    path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),name='password_reset_confirm',),
    path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'), name='password_reset_complete'),
    path('admin/', admin.site.urls),
    path('api/', include('api.urls'), ),
    path('users/', include('users.urls'), ),
]

下面是与DRF相关的API应用程序www.example.comurls.py that is related to DRF

app_name = 'api'

router = routers.DefaultRouter()
router.register(r'users', UserViewSet, basename='user')

urlpatterns = [
    path('', include(router.urls)),
    path('dj-rest-auth/', include('dj_rest_auth.urls')),
    path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
    path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

以下是模板密码_重置_确认. html

<main class="mt-5" >
            <div class="container dark-grey-text mt-5">
                <div class="content-section">
                    <form method="POST">
                        {% csrf_token %}
                        <fieldset class="form-group">
                            <legend class="border-bottom mb-4">Reset Password</legend>
                            {{ form|crispy }}
                            {{ form }}

                        </fieldset>
                        <div class="form-group">
                            <button class="btn btn-outline-info" type="submit">Reset Password</button>
                        </div>
                    </form>
                </div>
            </div>
        </main>

我的问题是:为什么窗体显示为NONE,如何修复它。

ldioqlga

ldioqlga1#

最有可能的是form不指向password_reset_confirm.html,因为默认情况下,django-rest-framework不包括PasswordResetConfirmViewcontext中的form
目前,您可以做的事情是通过在子类中继承PasswordResetConfirmView来创建自定义PasswordResetConfirmView,并使用form_class属性将form传递到模板上下文,因此:

from django.contrib.auth.forms import SetPasswordForm
from django.contrib.auth.views import PasswordResetConfirmView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters

class CustomPasswordResetConfirmView(PasswordResetConfirmView):
    form_class = SetPasswordForm
    success_url = reverse_lazy('password_reset_complete')
    template_name = 'users/password_reset_confirm.html'

    @method_decorator(sensitive_post_parameters('new_password1', 'new_password2'))
    @method_decorator(csrf_protect)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = self.form_class(user=self.request.user)
        return context

然后在urls.py中:

urlpatterns = [
    # .......
    path('password-reset-confirm/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    # .......
]

您可以根据需要提供任何success_url

相关问题