简体   繁体   中英

How to add my own python code in django in views.py index function

I have a code of python in different files: st.py and mydefinations.py, yahoo.py. And These codes are working fine but whenever I integrate this code in Django. It shows my error. This is my code of views.py in Django.

def index(request):
    latest_stocks_list = Stock.objects.all()
    template = loader.get_template('stocks/index.html')
    context = {
        'latest_stocks_list': latest_stocks_list,
    }
    return HttpResponse(template.render(context, request))

And I want to add the following code in the index function

from my definitions import *
from yahoo import *

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'}
tickerslist = ["AAPL"]
prntvar = {'prntnum': 0}
mydef = mydefinations()
for ticker in tickerslist:
    try:
        testdic = {'Var1': 0, 'Var2': 0}
        Ticker_stats = {}
        Ticker_summary = {}
        yahoo = my_yahoo(headers, ticker)
        Ticker_stats = yahoo.yahoo_stats()
        print(Ticker_stats)
    except Exception as e:
        pass

When I add code in the index and import line in the start and error shows that

my definitions module is not found

What should I do?

尝试像这样更新导入,

from your_app_name.mydefinations import *

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