TypeError:类型为'DeclarativeFieldsMetaclass'的对象没有len()

时间:2019-07-31 20:33:02

标签: python django forms typeerror

我正在尝试在Django中创建一个表单,用户在其中输入股票名称,然后使用它从Yahoo Finance API中获取数据。

我试图做同样的事情,但是得到了:TypeError:类型为'DeclarativeFieldsMetaclass'的对象没有len()。 帮帮我。 PS-我是编码新手

models.py

from django.db import models
from typing import List
import datetime as dt
from datetime import date
import pandas_datareader.data as web
import ta
from django.contrib.auth.models import User
from .forms import NameForm

X = NameForm

start = dt.datetime(2017, 1, 1)
end = date.today()
df = web.DataReader(X, 'yahoo', start, end)

close = df['Close']
high = df['High']
low = df['Low']
volume = df['Volume']

R = ta.rsi(close, n=14, fillna=False)
RSI = (R[-1])


class rsi(models.Model):
    status = ""
    if RSI <= 30:
        status = "Buy"
    elif RSI <= 15:
        status = "Buy"
    elif RSI >= 70:
        status = "Sell"
    elif RSI >= 85:
        status = "Sell"
    else:
        status = "Hold"

forms.py

from django import forms

class NameForm(forms.Form):
    Ticker = forms.CharField(label='Enter Ticker', max_length=100)

views.py

from .models import rsi, NameForm
from django.http import HttpResponse
from django.shortcuts import render

def get_ticker(request):
    if request.method == 'POST':
        ticker = NameForm(request.POST)
        if ticker.is_valid():
            return index(request)
    else:
        ticker = NameForm()
    return render(request, 'SITE/form.html', {'ticker': ticker})


def index(request):
    r = "RSI is: ", rsi.status
    return HttpResponse(r)

form.html

form action="/ticker/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

错误:

TypeError: object of type 'DeclarativeFieldsMetaclass' has no len()

0 个答案:

没有答案