Как правильно разрешить пост-запрос в django rest framework?
Теперь я получаю сообщение об ошибке при создании POST-запроса к api / v3 / exchange / order (используйте POST)
Traceback (most recent call last):
File "/home/skif/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/skif/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/skif/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/skif/PycharmProjects/beex2/app_ccxt/external_api.py", line 44, in post
return getattr(self, self.name)(request)
File "/home/skif/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/views.py", line 478, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/views.py", line 382, in initialize_request
parser_context=parser_context
File "/home/skif/.local/lib/python3.6/site-packages/rest_framework/request.py", line 160, in __init__
.format(request.__class__.__module__, request.__class__.__name__)
AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `app_ccxt.external_api.ApiExternalCCXT`.
[08/Jul/2019 15:40:31] "POST /api/v3/exchange/order HTTP/1.1" 500 44749
Я пытаюсь изменить экземпляр запроса на HttpRequest, но ничего не изменилось.
мой код в external_api.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpRequest
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from rest_framework import routers, serializers, viewsets
from beeqb.models import *
from rest_framework import status
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializer import *
from rest_framework.renderers import JSONRenderer
from beeqb.handlers.WalletHandler import select_wallet, get_crypt_wallet, make_sub_wallet
from django.db.models import Q, Sum
import requests
import json
from beeqb.handlers import ChartHandler, OrderHandler, WithdrawHandler, AddressHandler
from rest_framework import generics
from datetime import timedelta, datetime
import pytz
import time
from .api_lib import *
from beeqb.handlers.ChartHandler import *
from rest_framework.decorators import api_view, schema
from rest_framework.schemas import AutoSchema
from beeqb.models import Order, OrdersTrigger, ORDER_WAIT, ORDER_BUY, ORDER_SELL, ORDER_DONE
from django.template.context_processors import csrf
from rest_framework.views import APIView
from rest_framework.response import Response
import logging
class ApiExternalCCXT(APIView):
name = ''
def get(self, request, *args, **kwargs):
return getattr(self, self.name)(request)
def post(self, request, *args, **kwargs):
return getattr(self, self.name)(request)
@api_view(['POST', 'GET'])
def api_create_order(self, request):
“““
the method must accept POST requests
HERE SOME CODE
“““
def api_get_fetch_orders(self, request):
“““
the method accepted GET requests and working fine
HERE SOME CODE
“““
мой urls.py
from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from django.conf.urls import include, url
import beeqb
from .views import ApiCCXT, ApiOrder, Ticker, SymbolsDetails, ApiTransactions, ExampleView
from .serializer import *
from rest_framework.documentation import include_docs_urls
from app_ccxt.external_api import ApiExternalCCXT
import app_ccxt.ChartHandler
from app_ccxt.ApiCCXT.ChartHandlerCCXT import ApiChartHandlerCCXT
urlpatterns = [
“““ URLS “““
url(r'^exchange/order', ApiExternalCCXT.as_view(name='api_create_order')),
“““ URLS “““
url(r'^exchange/fetch_open_orders', ApiExternalCCXT.as_view(name='api_get_fetch_open_orders')),
“““ URLS “““
]
Если я попытаюсьвыполнить GET rquest для метода api_get_fetch_orders (exchange / fetch_open_orders) - все работает нормально.
Что я делаю не так?Как получить запрос на публикацию в методе api_create_order?Что не так в def post ()?
Я думаю, что все проблемы в post (), но не могу понять, как я могу изменить его для использования в нескольких методах.Мне нужен POST-запрос для этого метода и нескольких других
Я использую для GET-запроса: return getattr (self, self.name) (request)
Точно так же я хотел сделать с POSTreqursts.Но здесь у меня ошибка.
Если я использую только GET-запросы для метода api_create_order (я удалил нотации post () и @api_view из кода), все тоже работает нормально.
Можете ли вы помочь мне сэто?
UPD: POST / GET-запросы для метода api_create_order () с нотацией post () и @api_view.
Для запроса POST: Запрос информации:
USER: AnonymousUser
GET: No GET data
POST:
aaa = '123'
bbb = '234'
FILES: No FILES data
COOKIES: No cookie data
для запроса GET
Request information
USER AnonymousUser
GET
Variable Value
token '521f5013121d0829a946780cb5763ea41042c132'
number '1139012'
POST
No POST data