简体   繁体   中英

How do I resolve Django takes no arguments error I get when I plug in external api to project

I am a bit new to Django and Python in general

I am trying to consume an external API (alpacas get account) and return the result as json /rest

I tested the code in a simple python project without Django and it works perfectly, but when I plug it into Django, I get the takes no arguments error

After writing my code and running it, When I invoke the API in postman I see the takes no arguments error

see error details below

Django Version: 3.1.5 Exception Type: TypeError Exception Value:
AccountView() takes no arguments

Internal Server Error: /alpaca/v1/alpaca_account/
Traceback (most recent call last):
  File "/Users/xxxx/PycharmProjects/xxxx/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/xxxx/PycharmProjects/xxxx/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: AlpacaAccountView() takes no arguments
[27/Jun/2021 21:24:12] "GET /alpaca/v1/alpaca_account/ HTTP/1.1" 500 56001

Honestly, I have no idea what this error is talking about

Please help me resolve this

See My code below

View

import requests, json

from .config import *

BASE_URL = BASE_URL
ACCOUNT_URL = "{}/v2/account".format(BASE_URL)
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY}


# Create your views here.
class AccountView:
    def get_account(self):
        r = requests.get(ACCOUNT_URL, headers=HEADERS)

        return json.loads(r.content)

see urls.py

from django.urls import path, include

from AlpacaTrade.views import *

urlpatterns = [
    path('v1/alpaca_account/', AccountView),
]

models.py

from django.db import models


# Create your models here.

class AlpacaAccount(models.Model):
    symbol = models.CharField(max_length=200)
    qty = models.CharField(max_length=200)
    notional = models.CharField(max_length=200)
    side = models.CharField(max_length=200)
    type = models.CharField(max_length=200)
    time_in_force = models.CharField(max_length=200)
    limit_price = models.CharField(max_length=200)
    stop_price = models.CharField(max_length=200)
    trail_price = models.CharField(max_length=200)
    trail_percent = models.CharField(max_length=200)
    extended_hours = models.CharField(max_length=200)
    client_order_id = models.CharField(max_length=200)
    order_class = models.CharField(max_length=200)
    take_profit = models.CharField(max_length=200)
    stop_loss = models.CharField(max_length=200)
    status = models.CharField(max_length=200)
    limit = models.CharField(max_length=200)
    after = models.CharField(max_length=200)
    until = models.CharField(max_length=200)
    direction = models.CharField(max_length=200)
    nested = models.BooleanField(default=0)
    symbols = models.CharField(max_length=200)
    user_id = models.CharField(max_length=200)

Try using a function based view.

I'm not sure if this is 100% working because I didn't test it but it's a good start.

views.py

import requests, json
from django.http import JsonResponse
from .config import *

BASE_URL = BASE_URL
ACCOUNT_URL = "{}/v2/account".format(BASE_URL)
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY}


def get_account(request):
    r = requests.get(ACCOUNT_URL, headers=HEADERS)

    return JsonResponse(r.content, safe=False)

urls.py

from django.urls import path, include

from AlpacaTrade.views import *

urlpatterns = [
    path('v1/alpaca_account/', get_account),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM