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
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.
>
>
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
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
<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.,:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
$.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??
zalun
--
blog http://piotr.zalewa.info
jobs http://webdev.zalewa.info
twit http://twitter.com/zalun
face http://facebook.com/zaloon
#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
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),
)
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?
Thanks for all your help!!!
Bradley
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
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
Thanks Alec
On Thu, Sep 9, 2010 at 4:20 PM, Bradley Hintze