渲染视图后,如何做一些事情? (Django的) [英] How do you do something after you render the view? (Django)

查看:175
本文介绍了渲染视图后,如何做一些事情? (Django的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

return render_to_response()

信号是唯一的方法吗?我需要写一个自定义信号,还是请求完成给我足够的信息?基本上我需要知道什么页面呈现,然后采取行动来回应。

Are signals the only way to do this? Do I need to write a custom signal or does request_finished give me enough information? Basically I need to know what page was rendered, and then do an action in response to that.

谢谢。

从注释更新:我不想压缩页面的渲染,所以我想先渲染页面,然后执行操作。

UPDATE FROM COMMENTS: I don't want to hold up the rendering of the page, so I want to render the page first and then do the action.

推荐答案

你产生一个单独的线程并让它执行操作。

You spawn a separate thread and have it do the action.

t = threading.Thread(target=do_my_action, args=[my_argument])
# We want the program to wait on this thread before shutting down.
t.setDaemon(False)
t.start()

导致'do_my_action(my_argument)'在第二个线程中执行,即使在发送Django响应并终止初始线程之后,该线程也将继续工作。例如,它可以发送电子邮件,而不会延迟响应。

This will cause 'do_my_action(my_argument)' to be executed in a second thread which will keep working even after you send your Django response and terminate the initial thread. For example it could send an email without delaying the response.

这篇关于渲染视图后,如何做一些事情? (Django的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆