无法从Django视图返回到前端的响应

2024-04-18 12:46:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Django视图,它负责在文件被删除后返回响应值修改了。但是它什么也没收到。你知道吗

 import pyinotify
 from django.http import JsonResponse

 def get_all_data(request, *args, **kwargs):
   print('Started')
   print(os.path.join(BASE_DIR, "statics", "input"))
   wm = pyinotify.WatchManager()
   notifier = pyinotify.Notifier(wm)
   wm.add_watch("statics/input.txt", pyinotify.IN_MODIFY, onChange)
   notifier.loop()

   def onChange(ev): #this function will be executed when chnage detected
    f = open("statics/input.txt", "r")
    value = f.read()
    print('change detected')
    return JsonResponse({'foo': value})

我想要的是,当文件被修改时,在不刷新视图的情况下将该文件中的所有数据获取到Django模板。我使用ajax请求从后端获取数据。我不明白这里出了什么问题。你知道吗


Tags: 文件djangoimporttxt视图inputdefjsonresponse