Re: Redirect and execute after 2 seconds?

595 views
Skip to first unread message

Bill Freeman

unread,
Feb 27, 2013, 9:26:37 AM2/27/13
to django...@googlegroups.com
You must do this using JavaScript in the user's browser.  Look for time based events in JavaScript, including how to cancel them if a button is pressed.  The button should still probably submit to the Django server for recording, but you could simply record the fact that the correct (pseudo-)random id was visited.  You will have to record the GET for the time out case.  The time out handler can simple write to window.location to accomplish the "redirect" (which isn't really a redirect in the strict sense, since it doesn't occur as part of fetching a different page).  And that's JavaScript on the browser writing window.location, NOT anything within Django.

Bill

On Wed, Feb 27, 2013 at 9:01 AM, Maria <maria...@gmx.de> wrote:
Hi everyone! :)
 
I am working on a project for my studies, it is a web poll where you can only answer yes or no by clicking on buttons.
 
If the user clicked on one button, the result will be saved in a text-file and the user will be redirected to another page with a random id:
 
return HttpResponseRedirect ('/polls/'+str(sid))
 
If the user doesnt click on any button quickly enough, I want the page to redirect them after 3 seconds to another page with a random id AND i want to save the id of the question that wasnt answered.  How can I do it? I searched the web but only found the time.sleep function. As far as I understood, I cant use the sleep function because the site has to do other stuff while the 3 seconds pass.
 
I would be very thankful for any help on this!
 
Kind regards,
 
Maria

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Maria

unread,
Feb 27, 2013, 10:01:53 AM2/27/13
to django...@googlegroups.com
Thank you Bill! I dont know if I got you but if I do it with JavaScript I can't save the ID of the "missed" page, can I?
Because I want to save the page where no button was clicked to show it again at the end of the poll, so that the user gets another chance to answer this question.

Alan Plum

unread,
Feb 27, 2013, 10:06:05 AM2/27/13
to django...@googlegroups.com
Hi Maria,

as Bill explained, you should be looking at JavaScript for everything that happens on the client-side (i.e. after the page has left the server).

However you should consider shifting more of the logic to the client-side in this case. For time-based actions with such a short duration the additional overhead involved in sending and receiving a web page could affect the results quite dramatically, especially when the user is on a dodgy connection (think mobile or dial-up).

You may want to look into learning AJAX to communicate between the client and server without requiring a full page reload and into a front-end library like jQuery for adding the necessary client-side logic to hide and reveal elements on the page (i.e. client-side) or provide dynamic updates after the page has been sent by the server (Django).

There are some good live coding exercises and screencasts available online if you want to learn more about jQuery or other front-end libraries or just JavaScript in general.


Yours,

Alan


On 27.02.2013 15:01, Maria wrote:

Maria

unread,
Feb 27, 2013, 10:10:26 AM2/27/13
to django...@googlegroups.com
like this:
if user clicks button
  save answer
  return HttpResponseRedirect ('/polls/'+str(sid))
else after 3 seconds
  save this page for later

Maria

unread,
Feb 27, 2013, 10:19:41 AM2/27/13
to django...@googlegroups.com, m...@pluma.io
Hello Alan,
 
I'd love to learn something new but unfortunately I dont have the time since my project is due very soon. Is there no easy or short way to get this time variable into my code? Also, how do I "look for everything that happens on the client-side"? I am sorry for my incompetence.. :D

Roberto López López

unread,
Feb 27, 2013, 10:25:26 AM2/27/13
to django...@googlegroups.com

Maria, that code (your "after three seconds") has to be run on the
client, so django has nothing to do with it. As Bill suggests, you
should try some javascript here.



On 02/27/2013 04:10 PM, Maria wrote:
> _like this:_
> if user clicks button
> save answer
> return HttpResponseRedirect ('/polls/'+str(sid))
> else *after 3 seconds*
> save this page for later
> return HttpResponseRedirect ('/polls/'+str(sid))
>
> --
> 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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
Kind regards,

Roberto L�pez L�pez


System Developer
Parallab, Uni Computing
H�yteknologisenteret, Thorm�hlensgate 55
N-5008 Bergen, Norway
Tel: (+47) 555 84091

Bill Freeman

unread,
Feb 27, 2013, 10:44:45 AM2/27/13
to django...@googlegroups.com
Maria,

If the id is truely unique (is never used again for another version of the poll), then the fact that the browser visits the URL containing the ID gives you a chance to note the missed question.

Separately, you may want to look into Django's session stuff, since you then need not encode answer/non-answer IDs into the URL.  The session cookie associates a particular set of session data with requests from this browser.  Then all poll takers can share the same set of URLs, which might be unique to a question, or could be generic, with which question is currently being answered or missed stored on the session.  You might, in fact, use the same URL for everything, making the buttons submit buttons of a form, where which submit button is pressed is encoded in the query parameters, and the timeout submits to a hard coded URL that is either recognizable because it doesn't have that query parameter, or has the equivalent of a "timed out" or "skip" button hard coded in its query parameters.  Either way, using the session saves you from having to generate and match upon your "random" IDs.

Bill

Roberto López López

unread,
Feb 27, 2013, 10:45:12 AM2/27/13
to django...@googlegroups.com

Hi again Maria,

Try to combine the solutions proposed in those two pages:

http://stackoverflow.com/questions/2133166/loop-timer-in-javascript#2133217
http://stackoverflow.com/questions/948227/should-i-use-window-navigate-or-document-location-in-javascript#948242

That should be more than enough.

Roberto
>> *If the user doesnt click on any button quickly enough, I want the
>> page to redirect them after 3 seconds to another page with a
>> random id AND i want to save the id of the question that wasnt
>> answered.* How can I do it? I searched the web but only found the
>> time.sleep function. As far as I understood, I cant use the sleep
>> function because the site has to do other stuff while the 3
>> seconds pass.
>>
>> I would be very thankful for any help on this!
>>
>> Kind regards,
>>
>> Maria
>> --
>> 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 <javascript:>.
>> To post to this group, send email to django...@googlegroups.com
>> <javascript:>.
>> <http://groups.google.com/group/django-users?hl=en>.
>> For more options, visit https://groups.google.com/groups/opt_out
>> <https://groups.google.com/groups/opt_out>.
>>
>>
>
> --
> 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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


Maria

unread,
Feb 27, 2013, 11:00:35 AM2/27/13
to django...@googlegroups.com
Unfortunately, the ID is not unique, as every ID will be used 3 times in each session. Is it still possible to use JavaScript as a solution?

Bill Freeman

unread,
Feb 27, 2013, 11:08:23 AM2/27/13
to django...@googlegroups.com
Yes.  Tie it to the session.

Is the need for random IDs an attempt to keep users from figuring out how to re-answer questions by composing URLs themselves?  Just record which questions they have answered, and ignore attempts to answer them again.
Or are random IDs some PHB's beloved mechanism that you are stuck with?

Bill
Reply all
Reply to author
Forward
0 new messages