我试图从django webapp的views.py文件中的python代码中提取数据并将其输入到JSON文件中,但一旦运行服务器,我会得到prices.JSON和accountInfo.JSON文件的FileNotFoundError,尽管该文件所在的目录是正确的。我试着用“/filename”来表示它在当前目录中,但这也不起作用
from django.shortcuts import render
from .forms import ListForm
from django.http import HttpResponseRedirect
import json
robinUser = ''
robinPass = ''
capitalToInvest = 0
def MakeMoney(request):
if request.method == 'POST':
form = ListForm(request.POST)
if form.is_valid():
robinUser = form.cleaned_data['robinUser']
robinPass = form.cleaned_data['robinPass']
capitalToInvest = form.cleaned_data['capitalToInvest']
return HttpResponseRedirect('/index.html/')
else:
form = ListForm()
return render(request, 'home.html', {'form': form})
def getJSON(filePathAndName):
with open(filePathAndName, 'r') as fp:
return json.load(fp)
def overwriteJSON(data):
with open("./prices.json", 'w') as fp:
json.dump(data, fp)
def StartProgram(request):
rawJSON = getJSON('templates/prices.json')
rawJSON['capital'] = int(capitalToInvest)
with open('templates/prices.json', 'w') as fp:
json.dump(rawJSON, fp)
rawJSON2 = getJSON('templates/accountInfo.json')
rawJSON2['email'] = robinUser
rawJSON2['pass'] = robinPass
with open("templates/accountInfo.json", 'w') as fp:
json.dump(rawJSON2, fp)
return render(request, 'index.html')
以下是错误消息:
[Errno 2] No such file or directory: 'templates\\prices.json'
在此问题上的任何帮助都将不胜感激。提前谢谢你
您可以尝试将此JSON作为静态文件提供
请阅读文档中的更多内容: https://docs.djangoproject.com/en/3.0/howto/static-files/
相关问题 更多 >
编程相关推荐