如何使用 axios post 到我的 Django Rest 服务器?

How to use axios to post to my Django Rest server?

我在尝试从我的前端移动应用程序 post 到我的 Django 服务器时收到 403 错误。但是,当我 post 在浏览器上使用表单时,它工作得很好。

我成功地使用我的前端移动应用注册了一次,但从那以后,我收到了这个 403 错误。我已尝试注册和登录。

这是我在 Django 后端终端中遇到的错误:

Bad Request: /rest-auth/registration/
[16/Aug/2021 14:51:37] "POST /rest-auth/registration/ HTTP/1.1" 403 58

这是我的 settings.py 文件:


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

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',

    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'rest_auth.registration',

    'users',
]

SITE_ID = 1

.....


REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}

ACCOUNT_EMAIL_VERIFICATION = 'none'


这是我的前端(register.js):

    axios
      .post("http://127.0.0.1:8002/rest-auth/registration/", {
        username: "tester1234@gmail.com",
        email: "tester1234@gmail.com",
        password1: "tester1234@gmail.com",
        password2: "tester1234@gmail.com",
      })

我做错了什么?我只希望用户能够注册、登录和注销我的应用程序。

问题是我已经登录,然后没有提供密钥。

为了解决这个问题,我创建了一个注销按钮,然后添加了一种机制来保存密钥并在 header 中使用它。