Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在python文件中提取JSON数据时,django代码中显示FileNotFoundError_Python_Json_Django - Fatal编程技术网

试图在python文件中提取JSON数据时,django代码中显示FileNotFoundError

试图在python文件中提取JSON数据时,django代码中显示FileNotFoundError,python,json,django,Python,Json,Django,我试图从django webapp的views.py文件中的python代码中提取数据并将其输入到JSON文件中,但一旦运行服务器,我会得到prices.JSON和accountInfo.JSON文件的FileNotFoundError,尽管该文件所在的目录是正确的。我试着用“/filename”来表示它在当前目录中,但这也不起作用 from django.shortcuts import render from .forms import ListForm from django.http i

我试图从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作为静态文件提供

请阅读文档中的更多内容:

导入操作系统,然后将此os.path.join('templates/prices/json')改为。感谢您的帮助!