简体   繁体   English

如何使用django-rest-framework和React从django发送密码重置电子邮件

[英]How to send password reset email from django using django-rest-framework and React

I'm creating a website in the backend i'm using (django,django-rest-framework) and frontend (React.js). 我正在后端创建一个网站(django,django-rest-framework)和前端(React.js)。 I'm not clearly understand how do i create restfull api for password reset email. 我不清楚我如何为密码重置电子邮件创建restfull api。

The library Django Rest Auth (different from django rest framework's auth app) helps with this. Django Rest Auth库(与django rest framework的auth app不同)有助于此。 https://github.com/Tivix/django-rest-auth https://github.com/Tivix/django-rest-auth

Backend 后端

Install rest_auth into INSTALLED_APPS on the backend, and set up a URL rest_auth INSTALLED_APPS到后端的INSTALLED_APPS ,并设置URL

urlpatterns = [
  ...
  path('rest-auth/', include('rest_auth.urls')),
]

Frontend Once that's in place, you can send a post request to the password reset endpoint, and it'll send out an email using Django's built-in mail handling (I use django-anymail to get it to send through mailgun, but any email backend will work) 前端一旦到位,您可以向密码重置端点发送一个帖子请求,它将使用Django的内置邮件处理发送一封电子邮件(我使用django-anymail通过mailgun发送它,但任何电子邮件后端将工作)

This is a snippet from react-native, but you can use an equivalent web library like axios or jQuery to make the post request. 这是来自react-native的片段,但您可以使用像axios或jQuery这样的等效Web库来发出post请求。

  async resetPassword() {
    const { email } = this.state;
    fetch("https://mywebsite.com/api/v1/rest-auth/password/reset/", {
      method: "POST",
      body: JSON.stringify({email}),
    })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM