hi [django-users] How to do something after "return HttpResponse(html)"?

1,532 views
Skip to first unread message

lx

unread,
Jun 25, 2013, 6:35:31 AM6/25/13
to django...@googlegroups.com
hi all:
      I want to do someing after "return HttpResponse(html)".
for example
""""""""""""""
from django.http import HttpResponse
import datetime
def current_datetime(request):
          now = datetime.datetime.now()
          html = "<html><body>It is now %s.</body></html>" % now
          return HttpResponse(html)
"""""""""""""""""""""""""""""

Actually, I have a question that I need first give a response to the client, second I do something else.
How to solve this?

Thank you

Timster

unread,
Jun 25, 2013, 8:44:01 AM6/25/13
to django...@googlegroups.com
You cannot do something after the "return" line. This is Python functionality. When you return, the function exits and does not process anything else.

What exactly are you trying to do? There is probably another way to accomplish it.

Sandro Dutra

unread,
Jun 25, 2013, 8:55:20 AM6/25/13
to django...@googlegroups.com
The question is: What you're trying to do?

1. You can do any other process before the return line, the return will be the same... for example:

from django.http import HttpResponse
from datetime import datetime
def current_datetime(request):
          now = datetime.now()
          html = "<html><body>It is now %s.</body></html>" % now
          year = now.year()
          return HttpResponse(html)


2. You can use a conditional to have multiple returns:
from django.http import HttpResponse
from datetime import datetime
def current_datetime(request):
          now = datetime.now()
          html = "<html><body>It is now %s.</body></html>" % now
          year = now.year()
          if year == 2013:
              return HttpResponse(html)
         else:
              return HttpResponse(<html><body>Sorry it's not 2013.</body></html>")


2013/6/25 Timster <timsha...@gmail.com>
You cannot do something after the "return" line. This is Python functionality. When you return, the function exits and does not process anything else.

What exactly are you trying to do? There is probably another way to accomplish it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

lx

unread,
Jun 25, 2013, 9:32:59 AM6/25/13
to django...@googlegroups.com
Thank you.

The real demand is:

1.
receive the xml file from client.

2.
if (the xml format is ERROR) == True:
    return HttpResponse(<html><body>ERROR FORMAT</body></html>")
else:
    return HttpResponse(<html><body>RIGHT FORMAT</body></html>")
    #I must give a response first, And do other thing. Because the spend time of do_something() may be long.
    do_something(the xml file)






2013/6/25 Sandro Dutra <hex...@gmail.com>

Sandro Dutra

unread,
Jun 25, 2013, 9:46:36 AM6/25/13
to django...@googlegroups.com
If the "do_something" will take a long time to finish, you've 2 options: Find a better (optimized) way to "do_something" or inform your client the application is doing something and he will have to wait a little.

So, I can't see any advantage to say to your client there's something was ok if the application is not finish his work. If something goes wrong while "do_something" is working, your client will think everything was ok, but you'll an ocult error.


2013/6/25 lx <lxleno...@gmail.com>

Hector Armando Vela Santos

unread,
Jun 25, 2013, 10:30:13 AM6/25/13
to django...@googlegroups.com
You can use Celery for that, I've done something similar, just with a CSV instead of a XML, check this http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

once installed and configured, its as simple as:

from celery import task

if (the xml format is ERROR) == True:
    return HttpResponse(<html><body>ERROR FORMAT</body></html>")
else:
    do_something.delay(the_xml_file)
    return HttpResponse(<html><body>RIGHT FORMAT</body></html>")
    #I must give a response first, And do other thing. Because the spend time of do_something() may be long.

@task(ignore_result=True)
def do_something(xml):
    #do something time consuming


and the do_something function will be called in second plane, and the user will get an instant response, even if the parsing file process takes half an hour

lx

unread,
Jun 25, 2013, 11:44:05 PM6/25/13
to django...@googlegroups.com
Thank you.
But I want to fix it like this, because the time of "do something" is not long. 
#######################################
from django.http import HttpResponse
import datetime
def current_datetime(request):
          now = datetime.datetime.now()
          if now == 2013:

                          html = "<html><body>It is now %s.</body></html>" %
 now
                          return HttpResponse(html)
          else:
                         # giva a response first 
                         urllib.urlopen(message)
                         # do something 
                         print 'not 2013'
                         sys.exit(0)

#######################################


2013/6/25 Hector Armando Vela Santos <vell...@gmail.com>

Michael Anckaert

unread,
Jun 26, 2013, 4:18:13 AM6/26/13
to django...@googlegroups.com
You can't do anything after your view (= python function) returns.
If you need to perform a task but don't want to keep the user waiting,
you should look into Celery. This is a Distributed Task Queue: you can
start a task that a worker will execute in the backend. Your view can
return immediately, being very responsive to the user. In the meanwhile
the worker will perform the extra work in the background.

--
Kind regards
Michael Anckaert


signature.asc
Reply all
Reply to author
Forward
0 new messages