【问题标题】:NoReverseMatch at /api/users/request-reset-email/NoReverseMatch 在 /api/users/request-reset-email/
【发布时间】:2021-03-13 13:45:35
【问题描述】:

这不是重复的问题,我已经提到了旧问题,但没有找到答案。我正在使用 Django==1.10.5

我正在尝试使用 Django 休息框架来实现休息密码功能。但是我遇到了错误..

    Reverse for 'api/password_reset_confirm2' with arguments '('uidb64', 'token')' 
and keyword arguments '{}' not found. 0 pattern(s) tried: []

如果您查看模式搜索错误显示,它无法读取在 URL.py 中配置的任何 url 模式。这就是担心。

重置密码类:

class RequestPasswordResetEmailAPIView(GenericAPIView):
    serializer_class = ResetPasswordEmailSerializer

    def post(self,request):
        serializer = self.serializer_class(data=request.data)
        email = request.data.get('email','')
        if User.objects.filter(email=email).exists():
            user = User.objects.get(email=email)
            uidb64 = urlsafe_base64_encode(smart_bytes(user.id))
            token = PasswordResetTokenGenerator().make_token(user)
            current_site = get_current_site(
                request=request
            ).domain
            relativeLink = reverse('api/password_reset_confirm2'
                                   ,args={'uidb64':uidb64, 'token': token}
                                   )
            absurl = 'http://'+current_site+relativeLink
            email_body = 'Hello'+absurl
            data = {'email_body': email_body, 'to_email': user.email, 'email_subject': 'Reset password link' }
            Util.send_email(data)
        return Response('Success')

我的身份验证应用程序的 url.py 如下所示

urlpatterns = [
    url(r'^user/?$', UserRetrieveUpdateAPIView.as_view()),
    url(r'^users/?$', RegistrationAPIView.as_view()),
    url(r'^users/login/?$', LoginAPIView.as_view()),
    url(r'^users/request-reset-email/',RequestPasswordResetEmailAPIView.as_view(), name='users/request-reset-email'),
    url(r'^users/password-reset-confirm/(?P<uidb64>[-\w]+)/(?P<token>[-\w]+)/$', PasswordResetTokenCheckAPIView.as_view(), name='password_reset_confirm2'),
    # path('password-reset/<uidb64>/<token>/', RequestPasswordResetEmailAPIView.as_view(), name='api/password_reset_confirm2'),


]

在 Main url.py 中,url 是这样配置的

 url(r'^api/', include('apps.authentication.urls', namespace='authentication')),

请指教, 谢谢

【问题讨论】:

  • 我想我看到了一个错字relativeLink = reverse('api/password_reset_confirm2', ...)。您的网址在password_reset_confirm 之后没有2。也许这就是问题所在?
  • 通过删除和添加 2 我尝试过,我怀疑它可能与内置的 url 名称冲突,所以在最后添加了 2。但还是不行
  • 哦,好的。我这么说是因为很多 StackOverflow 问题都是由一个简单的错字引起的。
  • 我猜你的路径被评论了# path('password-reset/&lt;uidb64&gt;/&lt;token&gt;/'尝试删除#
  • 我没有使用 paht,而是使用 urlpatters

标签: django python-2.7 django-rest-framework


【解决方案1】:

尝试使用

relativeLink = reverse(
    'authentication:api/password_reset_confirm2',
    args={'uidb64':uidb64, 'token': token}
)

【讨论】:

  • 有些人会从天上掉下来拯救......谢谢......它解决了我的问题,但我不明白我做错了什么,你的解决方案是如何工作的:)
  • 就您使用unclude 而言,您需要显示包含的层次结构。这是官方文档docs.djangoproject.com/en/3.1/topics/http/urls/…
【解决方案2】:

您在 reverse 函数中的 URL 引用可能与您在另一个应用程序中的 urls 冲突。 而不是:

relativeLink = reverse('api/password_reset_confirm2'
                       ,args={'uidb64':uidb64, 'token': token}
                      )

试试这个:

relativeLink = reverse('password_reset_confirm2'
                                   ,args={'uidb64':uidb64, 'token': token}
                                   )

现在您通过其名称引用 URL。

【讨论】:

  • 出现了同样的错误,“password_reset_confirm2”的反向参数“('uidb64','token')'和关键字参数'{}'未找到。尝试了 0 个模式:[]
猜你喜欢
  • 1970-01-01
  • 2015-04-09
  • 2023-03-24
  • 1970-01-01
  • 2015-12-03
  • 2018-07-18
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多