【发布时间】:2023-03-16 02:38:01
【问题描述】:
我用 vuejs 和 django rest 框架创建了一个 rest api。问题是当我提出发布请求时。 通过获取请求,他可以工作,但不能通过发布请求。
axios
.get('http://127.0.0.1:8000/api/users/')
.then(response => (this.info = response.data))
axios
.post('http://127.0.0.1:8000/api/users/', {
params : {
email : "test@gmail.com",
username : 'test'
}
})
.then(response => (this.info = response.data))
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
urlpatterns = [
path('', include(router.urls)),
]
我的 get 请求有效,但我的 post 请求无效。在我的控制台中,我有:http://127.0.0.1:8000/api/users/?username=test&email=test@gmail.com 400(错误请求) 当我观看网络时,我有 {"username":["This field is required."]}
我不明白为什么会出现这个错误。
【问题讨论】:
标签: django vue.js django-rest-framework axios