如何在Django中的功能中使用两次返回?
我有一个称为响应的模型。如果选择用户持续一段时间,则响应在此持续时间后转向默认值。但是我需要返回值,直到持续时间结束。我认为我需要使用异步功能。我看了看。但找不到任何Solutioın。
Models.py
class Response(models.Model):
def_res = models.CharField()
expected_res = models.CharField()
current_res = models.CharField()
is_duration= models.BooleanField(default=False)
duration = models.PositiveIntegerField()
Views.py
def manage_response(request):
if request.method == "POST":
a = Response.objects.filter(id=request.POST.get('resp_id'))
a.current_res = a.expected_res
a.save()
if not a.is_duration == True:
return JsonResponse(a.current_res)
else:
return JsonResponse(a.expected_res )
#after return won't work in sync
time.sleep(a.duration)
a.current_res = a.def_res
a.save()
return JsonResponse(a.current_res)
I have a model called response. If the user is selected for a duration the response is turning to the default value after that duration time. But I need to return the value until the duration time is over. I think I need to use the async function. I looked at the SO. but couldn't find any solutioın.
models.py
class Response(models.Model):
def_res = models.CharField()
expected_res = models.CharField()
current_res = models.CharField()
is_duration= models.BooleanField(default=False)
duration = models.PositiveIntegerField()
views.py
def manage_response(request):
if request.method == "POST":
a = Response.objects.filter(id=request.POST.get('resp_id'))
a.current_res = a.expected_res
a.save()
if not a.is_duration == True:
return JsonResponse(a.current_res)
else:
return JsonResponse(a.expected_res )
#after return won't work in sync
time.sleep(a.duration)
a.current_res = a.def_res
a.save()
return JsonResponse(a.current_res)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在通过
字典
中使用JSONRESPONSE
。You can use
dictionary
when passing throughJsonResponse
.