Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do something after you render the view? (Django)

Tags:

python

django

I want to do something after I have rendered the view using

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.

Thanks.

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.

like image 627
rick Avatar asked Jul 04 '09 02:07

rick


People also ask

How does render work in Django?

render() Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. Django does not provide a shortcut function which returns a TemplateResponse because the constructor of TemplateResponse offers the same level of convenience as render() .

What type of object should be returned in Django view?

A view in Django must return a HttpResponse , even if it's empty.

What Does views PY do in Django?

In the Django framework, views are Python functions or classes that receive a web request and return a web response. The response can be a simple HTTP response, an HTML template response, or an HTTP redirect response that redirects a user to another page.


1 Answers

A common way to do this is to use message queues. You place a message on the queue, and worker threads (or processes, etc.) consume the queue and do the work after your view has completed.

Google App Engine has the task queue api http://code.google.com/appengine/docs/python/taskqueue/, amazon has the Simple Queue Service http://aws.amazon.com/sqs/.

A quick search didn't turn up any django pluggables that look like accepted standards.

A quick and dirty way to emulate the functionality is to place the 'message' in a database table, and have a cron job periodically check the table to perform the work.

like image 53
dar Avatar answered Sep 18 '22 13:09

dar