Please wait page trouble

287 views
Skip to first unread message

Bradley Hintze

unread,
Aug 30, 2010, 7:18:48 PM8/30/10
to django...@googlegroups.com
I am attempting to do a lengthe calculation that will require the user
to wait a bit. I want a 'Please wait page to come up while the lengthy
calculation is performed. I thought this might work:

views.py

def please_wait(request):
return HttpResponse('Please Wait......')

def run_DHM(request):
please_wait(request)
....lengthy calculations...


This did not show the 'Please Wait' page. Is there a better way to do
what I am trying to do?

--
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799

Rolando Espinoza La Fuente

unread,
Aug 30, 2010, 10:37:21 PM8/30/10
to django...@googlegroups.com
On Mon, Aug 30, 2010 at 7:18 PM, Bradley Hintze
<brad...@aggiemail.usu.edu> wrote:
> I am attempting to do a lengthe calculation that will require the user
> to wait a bit. I want a 'Please wait page to come up while the lengthy
> calculation is performed. I thought this might work:
>
> views.py
>
> def please_wait(request):
>    return HttpResponse('Please Wait......')
>
> def run_DHM(request):
>    please_wait(request)
>    ....lengthy calculations...
>
> This did not show the 'Please Wait' page. Is there a better way to do
> what I am trying to do?
>

You are not returning the HttpResponse object from please_wait(). But anyway,
doesn't work that way. At least with django.

What you can do is render a normal html with the message "Please wait",
then perform an ajax call to start the calculations and finally return
a json response
to display the result in the client-side.

Roughly:

def please_wait(request):
# ... setup context or something
return render_to_response("please_wait.html")

def run_DHM(request)
# ... perform calculations and collect the result in a dict
data = {"result": something}
return HttpResponse(json.dumps(data), mimetype="application/json")


# using jquery in your html
<script type="text/javascript">
$.getJSON("/run_DHM/", function(data) {
// do something with result
console.log(data.result);
});
</script>


Rolando Espinoza La fuente
www.insophia.com

> --
> Bradley J. Hintze
> Graduate Student
> Duke University
> School of Medicine
> 801-712-8799
>

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Bradley Hintze

unread,
Aug 31, 2010, 10:38:22 AM8/31/10
to django...@googlegroups.com
I'll look into this. I have no idea what you mean by 'ajax' or 'json'.
Thus your code doe'snt really make sense given my lack of knowlege. I
will do some googling to see if I can piece it together. Thanks for
the help!

Bradley Hintze

unread,
Aug 31, 2010, 2:46:01 PM8/31/10
to django...@googlegroups.com
I'm getting no where with this, are there any other suggestions on row
to render a 'please wait' page while other python works? Can I call a
view.my function to run from my 'please wait' template?

Please be as elementary as possible as javascript, AJAX, jquery, and
the like are brand new to me.

Thanks,
Bradley

On Tue, Aug 31, 2010 at 10:38 AM, Bradley Hintze

Alec Shaner

unread,
Aug 31, 2010, 4:12:46 PM8/31/10
to django...@googlegroups.com
Rolando's suggestion is a pretty straight forward method to achieve what you want. You probably need to elaborate where in the process you're having trouble (although based on your response maybe it's the first step?). You can indeed call a view.my function from your please wait template, but you want to do it in the background, i.e., with an AJAX request in order to keep the Please Wait message displayed while your server processes the request.

Bradley Hintze

unread,
Aug 31, 2010, 5:11:19 PM8/31/10
to django...@googlegroups.com
Ok, I'll try to elaborate. again java is foreign to me although I've
spent all day trying to learn some.
Here's the code:

def please_wait(request):
# ... setup context or something
return render_to_response("please_wait.html")

def run_DHM(request)
# ... perform calculations and collect the result in a dict
data = {"result": something}
return HttpResponse(json.dumps(data), mimetype="application/json")


# using jquery in your html
<script type="text/javascript">
$.getJSON("/run_DHM/", function(data) {
// do something with result
console.log(data.result);
});
</script>

def run_DHM(request) is straight forward then it falls apart.

$.getJSON("/run_DHM/", function(data) {
// do something with result
console.log(data.result);
});

I presume that '$.getJSON("/run_DHM/", function(data)' somehow runs
the run_DHM function? How can it even find the function since it lives
in view.py? '// do something with result'???? I want to send another
page to the client with the results but do I do this here? If not,
what do I do here? What does 'console.log(data.result);' do? Why does
def run_DHM(request) return a HttpResponse? I have a template I want
it to return so shouldn't I use render_to_response? Obviously, I am
not getting how the javascript works so I have no idea how to set this
up. I was hoping there was a pythonic solution :).

I hope this explains my utter confusion sufficiently.

Bradley

Alec Shaner

unread,
Aug 31, 2010, 7:37:08 PM8/31/10
to django...@googlegroups.com
$.getJSON should be embedded in your initial Page Wait response page. The first argument isn't a view, rather a URL. So you might want to use the django url template tag, e.g.,

$.getJSON('{% url whatever.run_DHM %}', ...)

The second argument is a callback function. The browser stores the callback and subsequently calls it when the url requested returns a response. It also might be confusing because the function is defined inline (anonymous), which is a frequent shortcut used in javascript. But anyway, when the callback function executes he just put console.log(result.data) as way to see what gets returned.

 You say you want to send the user to another page with the results, but you could return a rendered chunk of html code and replace the Please Wait message using jquery to mainupulate the DOM (jquery documentation has tutorials). Or if you want to redirect to a new page you could store the calculation results in the session and redirect to a view that pulls from that same session data (or use memcached). So maybe instead of console.log(result.data), you could use window.location="/some/url/to/show/calculations/";

His example returns a HttpResponse just to illustrate the concept of how to return JSON data. You don't have to return JSON, but it's a good method if you want to return structured data. In the above example of a redirect you could just return anything I suppose, i.e., just an 'OK'.

Bradley Hintze

unread,
Sep 7, 2010, 10:31:28 AM9/7/10
to django...@googlegroups.com
It seems as if this is the only solution but after a week of trying to
implement it in a variety of ways it won't work. is there something I
need to download (jquery)? More helpful would be a real HTML page as
an example or a live example on the web. I am sorry for the trouble
but I'd like to understand this and get it working.

Alec Shaner

unread,
Sep 7, 2010, 1:36:40 PM9/7/10
to django...@googlegroups.com
You need to include the jquery library script in your html page if you're calling getJSON as in the previous emails (and note that getJSON isn't necessarily the function you want - jquery provides many options for doing AJAX). Either download it, or use one of the public repositories:

http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery

In fact, just view the html source code for the link just referenced - you'll see how jquery is included:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
Then I'd recommend one of the tutorials, e.g.,:

http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

The tutorials should serve as live examples.

Bradley Hintze

unread,
Sep 8, 2010, 6:56:23 PM9/8/10
to django...@googlegroups.com
This is what I have in my please_wait.html

$.getJSON('{% url run_DHM %}')
});
</script>

This is the django error

Caught NoReverseMatch while rendering: Reverse for 'run_DHM' with
arguments '()' and keyword arguments '{}' not found.

run_DHM takes 'request' as an argument. How do I pass it the argument??

Piotr Zalewa

unread,
Sep 8, 2010, 7:34:15 PM9/8/10
to django...@googlegroups.com
Do you have it in the urls.py file?
Would be best to paste it into pastebin or here if it's few lines only

zalun


--
blog http://piotr.zalewa.info
jobs http://webdev.zalewa.info
twit http://twitter.com/zalun
face http://facebook.com/zaloon

Brian Neal

unread,
Sep 8, 2010, 9:34:38 PM9/8/10
to Django users
On Sep 8, 5:56 pm, Bradley Hintze <bradle...@aggiemail.usu.edu> wrote:
> This is what I have in my please_wait.html
>
> <script type="text/javascript"
> src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
> $.getJSON('{% url run_DHM %}')});
>
> </script>

I don't think that is right, is it? At least I've never seen it done
that way. I think you need two script tags:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
// Your javascript goes here...
</script>

>
> This is the django error
>
> Caught NoReverseMatch while rendering: Reverse for 'run_DHM' with
> arguments '()' and keyword arguments '{}' not found.
>
> run_DHM takes 'request' as an argument. How do I pass it the argument??
>

All views take request as a first argument. But that isn't the
problem. Please post your urls.py that has your run_DHM view in it.

Regards,
BN

Bradley Hintze

unread,
Sep 9, 2010, 8:45:25 AM9/9/10
to django...@googlegroups.com
#url.py
...
(r'^please_wait/', please_wait),
(r'^DHM_run/$', run_DHM),
...

#please_wait.html

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
$.getJSON('{% url run_DHM %}')
});
</script>

#view.py
def please_wait(request):
c = {'we_r_home':'yes'}
return render_to_response('please_wait.html', c)

def run_DHM(request):
//put calculated data in request.session
return render_to_response('DHM_ran.html', request.session,
context_instance=RequestContext(request))


This is the django error

Caught NoReverseMatch while rendering: Reverse for 'run_DHM' with
arguments '()' and keyword arguments '{}' not found.

run_DHM takes 'request' as an argument. How do I pass it the argument??

Bradley

Alec Shaner

unread,
Sep 9, 2010, 9:22:56 AM9/9/10
to django...@googlegroups.com
Could you post the full url.py file?

And as Brian mentioned your javascript block should be separated. Plus you have an extra }); that's going to fail once you resolve this reverse error. It's also not clear what you intend to happen when run_DHM returns its response? It looks like your intent is to do a redirect and run_DHM will use session data to retrieve calculation results.

Just to clarify, I think your plan is to:

1. Display a Please Wait page that fires off an AJAX call to some view that performs calculations => run_DHM. The results of the calculations are stored in the session.
2. run_DHM returns a simple response that will indicate sucess, e.g., "OK"
3. Redirect to a view to display the results, which are retrieved from the session => display_DHM

I think you need three views here: please_wait, run_DHM, and display_DHM.

Bradley Hintze

unread,
Sep 9, 2010, 9:59:03 AM9/9/10
to django...@googlegroups.com
Alec,

Thanks for your patience. The jquery tutorials have been frustrating.
Anyway, I do not have three 'views' as you suggested. I will try that.
But I need to understand a few things before I try that. How to call
run_DHM from my please_wait.html page. (I assume AJAX but I've tried
and tries what have been suggested with no success, most likely due to
my failed attempts at understanding AJAX) I assume after I run the
run_DHM view function I will somehow have run_DHM redirect it to the
display_DHM. My question is, how do I redirect AND pass the
request.session arguments, which is where the data from run_DHM will
be stored?

As requested, here is my full url.py:

from django.conf.urls.defaults import *
from MolProbity_Compare_test.views import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
(r'^home/$', home_view),#the 'index' or home or top page view
(r'^about/$', about_view),
(r'^log_out_confirm/$', log_out_confirm),
(r'^log_out/$', log_out),
(r'^upload/$', uploaded_PDBs),
(r'^rotamer_diff/$', rotamer_dif_frame),
(r'^side-by-side/$', side_by_side),
(r'^side-by-side-key/$', side_by_side_key),
(r'^side-by-side-frame/$', side_by_side_frame),
(r'^DHM_run/$', run_DHM),
(r'^please_wait/', please_wait),
(r'^analyze/$', analyze_compare),
)

Alec Shaner

unread,
Sep 9, 2010, 10:37:34 AM9/9/10
to django...@googlegroups.com
I feel your pain - javascript has always been a pain for me, but libraries like jQuery make it more bearable!

AJAX calls are made in the background (usually asynchronously), so they don't directly change the page you're currently viewing in your browser. The browser is responsible for dispatching the response returned from your AJAX call, typically by invoking a callback function you specified when the AJAX request was initiated. So you don't do a redirect from the server, rather you can do it using client side javascript in your callback function.

Here is how it basically works:
1. Please Wait page makes AJAX request to run_DHM, specifies callback function when request completes.
2. run_DHM performs calculations, stores results in session, returns an "OK" response.
3. Browser invokes the callback function specified in step 1, which should change the page to display_DHM view.
4. display_DHM retrieves calculation results from session and renders HTML page.

Your first problem is with the url template tag. You might try this:

{% url MolProbity_Compare_test.views.run_DHM %}

You can also use a named urls, e.g.,
...
url(r'^DHM_run/$', run_DHM, name="run_DHM"),
...
in which case {% url run_DHM %} should also work

Or you can just call the url directly instead of using the url template tag, e.g., '/run_DHM/'.

The request argument is always passed to your views, you don't have to do it explicitly. So your run_DHM and display_DHM views will always have that available. Just get the session out of the request object in each view you need it.

Basic skeleton:

# please_wait.html
...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
    $.get('{% url run_DHM %}', function(data) {
        if (data == 'OK') {
              window.location.href = '{% url display_DHM %}';
        } else {
              alert(data);
        }
    });
</script>

Note I've used the shorthand urls above, you could just use '/run_DHM/' and '/display_DHM/' instead of the url template tags. You could also use the full package path. Also note that the callback function I refer to is defined inline - the function(data) ... part.

# views.py
from django.http import HttpResponse

def please_wait(request):
    return render_to_response('please_wait.html', ...)

def run_DHM(request):
    # Do calculations, store results in request.session
    ...
    # If everything ok, return OK (otherwise return some error)
    return HttpResponse('OK')

def display_DHM(request):
    # Get results from session
    return render_to_response('ran_DHM.html', ...)

This is over simplified, but should serve to get started if you want to use this redirect approach.

... 

Joe Casper

unread,
Sep 9, 2010, 10:38:02 AM9/9/10
to django...@googlegroups.com
The NoReverseMatch error is being thrown because Django cannot find a named URL pattern to match {% url run_DHM %}.

Here is the link to the spot in the docs where it discusses this feature:
http://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns

In essence, you would need to rewrite your URL pattern to look like the following:

url(r'^DHM_run/$', run_DHM, name="run_DHM")

Any time you use the {% url %} template tag it needs to map to a single named URL pattern.

Hope this helps.

joe_casper

Bradley Hintze

unread,
Sep 9, 2010, 11:05:28 AM9/9/10
to django...@googlegroups.com
Thanks Alec,

That finally makes sense. However, I do have a question here:

def display_DHM(request):
# Get results from session
return render_to_response('ran_DHM.html', ...)

'# Get results from session'???? Would I not just do this:

def display_DHM(request):
return render_to_response('DHM_ran.html', request.session, ...)

Or do I have to explicitly get the session data? If so, how?

Alec Shaner

unread,
Sep 9, 2010, 11:17:55 AM9/9/10
to django...@googlegroups.com
That's really a design issue up to you, i.e., how you get data from your view to your template. Since I don't know the format or how much data you're storing in the session it's hard to say. I was just assuming you'd use context variable(s) to pass the data to the template. I'm not intimately familiar with the Session API, but isn't it just a dictionary?

Bradley Hintze

unread,
Sep 9, 2010, 11:23:39 AM9/9/10
to django...@googlegroups.com
Yeah, I just tried out what I wrote in my last and it worked!

Thanks for all your help!!!

Bradley

Bradley Hintze

unread,
Sep 9, 2010, 12:00:42 PM9/9/10
to django...@googlegroups.com
OK,

Got it working. Sorry one more question. I have a couple of places
where I'd like to display the 'Please Wait' page. I'd imagine I'd do
something similar to the following::

# please_wait.html
...
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">

if (If_come_from_pageA) {


$.get('{% url run_DHM %}', function(data) {
if (data == 'OK') {
window.location.href = '{% url display_DHM %}';
} else {
alert(data);
}
}

else if (If_come_from_pageB) {
$.get('{% url run_analysis %}', function(data) {


if (data == 'OK') {

window.location.href = '{% url display_analysis %}';
} else {
alert(data);
}
}
});
</script>

an of course configure url.py and view.py as explained previously. Is
there an easy way to do this? In other words, what are the
If_come_from_pageA and If_come_from_pageB conditions? Can I access the
context dictionary that I passed to please_wait.html?

On Thu, Sep 9, 2010 at 11:23 AM, Bradley Hintze

Alec Shaner

unread,
Sep 9, 2010, 12:44:11 PM9/9/10
to django...@googlegroups.com
A lot of ways to do this. Yes you could do it as above, but that can get ugly as you start adding more views. You can indeed access the context dictionary in please_wait.html. It's hard to say without knowing more details about your application. How does the user pass arguments (if any) used in these calculations? If you're looking for a simple way to abstract this process, consider using URL confs:

# Create a url specific to the DHM calulcation
url(r'^DHM_run_start/$', please_wait, kwargs={'calculation_url': 'run_DHM'}, name='run_DHM_start'),

and you'd call it like /DHM_run_start/

or

# Create a URL with a regex pattern to capture a calculation_url
url(r'^please_wait/(?P<calculation_url>[\d\w]+)/$', please_wait),

and you'd call it like /please_wait/run_DHM/

The second option is more generic, but you'll need to validate the url argument. The first option means adding more url entries. In either case your please_wait view would be defined like:

def please_wait(request, calculation_url):
    return render_to_response('please_wait.html', {'calculation_url': calculation_url}, ...)

please_wait.html could then substitute the {{ calculation_url }} context variable as the argument to the {% url %} tag.

Your run_DHM view could now return the url to redirect to instead of just 'OK'. You'll have to return the full URL, e.g., '/display_DHM/', from your run_DHM view. This is probably where you'd start thinking about a JSON response as you might return a dictionary with an result code ('OK' or 'error') along with a url to redirect the user to.

window.location.href = data;

Or if you've switched to a JSON response it might be data.display_url after checking that data.result = 'OK'.

Hope that all makes sense.

Bradley Hintze

unread,
Sep 9, 2010, 4:20:28 PM9/9/10
to django...@googlegroups.com
Ok, cant quite get it. Here is what I have:

please_wait.html
...
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">

$.get({{ calculation_url }}, function(data) {


if (data == 'OK') {

window.location.href = {{ view_url }};
}
else {
alert(data);
}
});
</script>
...

url.py


url(r'^DHM_run_start/$', please_wait, kwargs={'calculation_url':

'/run_DHM/', 'view_url':'/DHM_display/'}, name='run_DHM_start'),
url(r'^analyze_start/$', please_wait, kwargs={'calculation_url':
'/analyze_compare/', 'view_url':'/results_display/'},
name='analyze_start'),

view.py
...
def please_wait(request, calculation_url, view_url):
c = {'calculation_url': calculation_url, 'view_url': view_url}
return render_to_response('please_wait.html', c,
context_instance=RequestContext(request))

def run_DHM(request):
#run calculations
return HttpResponse('OK')


def analyze_compare(request):
#run calculations
return HttpResponse('OK')
...

I could have run_DHM and analyze_compare return views rather than the
HTTPResponse "OK" as you indicated but was unsure how to change the
javascript to make it work. This seems like it should work but I just
get the "please_wait" page indefinitely. Seems as if its not calling
{{ calculation_url }}.

Any ideas or elaborations (hold my hand) on JSON would be great.

Thanks,
Bradley

Bradley Hintze

unread,
Sep 10, 2010, 8:54:13 AM9/10/10
to django...@googlegroups.com
I got to work! I needed a good nights sleep to see it. the url was
'/DHM_run/' NOT '/run_DHM/'.

Thanks Alec

On Thu, Sep 9, 2010 at 4:20 PM, Bradley Hintze

Alec Shaner

unread,
Sep 10, 2010, 9:13:53 AM9/10/10
to django...@googlegroups.com
Excellent. Glad you got it working.
Reply all
Reply to author
Forward
0 new messages