【问题标题】:How to use axios to post to my Django Rest server?如何使用 axios 发布到我的 Django Rest 服务器?
【发布时间】:2021-08-18 21:47:13
【问题描述】:

我在尝试从我的前端移动应用发布到我的 Django 服务器时收到 403 错误。但是,当我在浏览器上使用表单发布时,它完全可以正常工作。

我曾经使用我的前端移动应用注册过一次,但从那以后,我收到了这个 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",
      })

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

【问题讨论】:

  • 您是否记得将身份验证路径添加到您的 urls.py 中?

标签: django authentication django-rest-framework axios django-rest-auth


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2023-03-26
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多