我有两个视图集: UserViewSet和GoogleViewSet。
在GoogleViewSet中,我想验证令牌,然后重定向到UserViewSet。
class GoogleViewSet(APIView):
def post(self, request):
# some logic
password = User.objects.make_random_password()
return redirect(reverse('v1_users:user_create'),
kwargs={'email': 'test@test.com', 'username': 'test', 'password': password})
但是在重定向时,我得到了405错误
{
"detail": "Method \"GET\" not allowed."
}
如何重定向为post请求?
我已经尝试过了,但得到的request.data为空
class UserViewSet(ViewSet):
def create(self, request, *args, **kwargs):
data = request.data
return Response()
class GoogleViewSet(APIView):
permission_classes = (permissions.AllowAny,)
def post(self, request):
password = User.objects.make_random_password()
return HttpResponseTemporaryRedirect(
'/v1/users/',
content=json.dumps({'email': 'test@test.com', 'username': 'test', 'password': password})
)
发布于 2021-09-16 09:10:45
为此,您需要状态代码307。通常重定向会给你302,除非它是永久重定向,在这种情况下是301。两者都将请求转换为GET。
307类似于302,但保留了请求方法和主体。
你可以像这样继承HttpResponseRedirectBase
的子类:
class HttpResponseTemporaryRedirect(HttpResponseRedirectBase):
status_code = 307
并在视图中使用此响应类
https://stackoverflow.com/questions/69205313
复制相似问题