How do i redirect to a different view

785 views
Skip to first unread message

JagChris

unread,
May 7, 2011, 2:16:22 PM5/7/11
to web2py-users
I want after the user clicks the submit button that he/she gets
redirect to a different view how do i do that?

Jonathan Lundell

unread,
May 7, 2011, 2:23:28 PM5/7/11
to web...@googlegroups.com
On May 7, 2011, at 11:16 AM, JagChris wrote:
>
> I want after the user clicks the submit button that he/she gets
> redirect to a different view how do i do that?

redirect(URL(whatever)) in your form.accepts clause

JagChris

unread,
May 7, 2011, 2:34:31 PM5/7/11
to web2py-users
so if the next view is default/other.html

it would be redirect(URL('other'))?

Anthony

unread,
May 7, 2011, 2:46:08 PM5/7/11
to web...@googlegroups.com
On Saturday, May 7, 2011 2:34:31 PM UTC-4, JagChris wrote:
so if the next view is default/other.html

it would be redirect(URL('other'))?
 
Yes, though keep in mind that in web2py, you're not technically calling or redirecting to *views*. Rather, you are redirecting to a *function* (i.e., action) in a controller, and the function has a view associated with it (or if it doesn't, the generic view is used). So, if you do redirect(URL('other')), it will redirect to the other() function in the default.py controller, which will ultimately be rendered with the 'other.html' view (if it exists). For more details, see http://www.web2py.com/book/default/chapter/04#Dispatching.
 
Anthony

Jonathan Lundell

unread,
May 7, 2011, 2:46:52 PM5/7/11
to web...@googlegroups.com
On May 7, 2011, at 11:34 AM, JagChris wrote:
>
> so if the next view is default/other.html
>
> it would be redirect(URL('other'))?

Right, assuming that the current function is in default. Otherwise redirect(URL('default', 'other'))

JagChris

unread,
May 7, 2011, 3:08:50 PM5/7/11
to web2py-users
##this block is in my index function :
json_gotten = simplejson.load(json_data)
redirect(URL('mathworksheet'))
#redirect(URL(r=request,f='manageJSON',args=json_gotten))
return dict(passedin=json_gotten)
## then i have this
def mathworksheet():
return dict(message=T('Hello World'),
number_of_problems=json_gotten["number_of_problems"],
questions=json_gotten["questions"])
and i get an error that json_gotten is not defined?? so help

Anthony

unread,
May 7, 2011, 3:27:33 PM5/7/11
to web...@googlegroups.com
On Saturday, May 7, 2011 3:08:50 PM UTC-4, JagChris wrote:
##this block is in my index function :
json_gotten = simplejson.load(json_data)
        redirect(URL('mathworksheet'))
        #redirect(URL(r=request,f='manageJSON',args=json_gotten))
        return dict(passedin=json_gotten)
 
It looks like this will always redirect to 'mathworksheet', so the index function will never return the dictionary with json_gotten. Once the redirect happens, the function is exited.
 
number_of_problems=json_gotten["number_of_problems"],
questions=json_gotten["questions"])
 
Where are the above two lines? Are they in the 'mathworksheet' function, or in a view? If the former, note that you never passed the json_gotten variable from the index function to the mathworksheet function. You simply redirected from index to mathworksheet, but that starts a new request.
 
What exactly are you trying to do?
 
Anthony

Anthony

unread,
May 7, 2011, 3:33:17 PM5/7/11
to web...@googlegroups.com
def mathworksheet():
    return dict(message=T('Hello World'),
number_of_problems=json_gotten["number_of_problems"],
questions=json_gotten["questions"])
 
Sorry, I missed the comma on the second line -- now I see it's all part of the returned dictionary of mathworksheet. Same problem, though. json_gotten is defined in the index function, which simply redirects to mathworksheet (which starts a new request, so json_gotten is lost).
 
Why are you bothering with the redirect, though? It looks like mathworksheet just returns a dictionary of stuff that is generated in the index function -- so, why not just have the index function return the dictionary?
Anthony

JagChris

unread,
May 7, 2011, 4:16:03 PM5/7/11
to web2py-users
they are in the mathworksheet function...i paste everything below...i
want that after i slick submit on my form it redirects to another view
called mathworksheet where it will look to display everything from the
json

import urllib
from gluon.contrib import simplejson as json
#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does
streaming)
## - call exposes all registered services (none by default)
#########################################################################

def index():

if request.vars :
quest_types = request.vars.typeofques
quest_num = request.vars.numofques
RetURL="http://mathmakerapi.appspot.com/cs34wmathmaker/default/
worksheet.json?numberques="+quest_num+"&questype="+quest_types
json_data = urllib.urlopen(RetURL)
jsonresult = simplejson.load(json_data)
session.jresult = jsonresult
#redirect(URL('mathworksheet'))
redirect(URL(r=request,f='mathworksheet',args=jresult))

return dict(passedin=jsonresult)


return dict(passedin="")


## return dict(RetURL=redirectedurl,posts=db().select(db.post.ALL))
def mathworksheet():
# from gluon.contrib import simplejson
url = 'http://127.0.0.1:8000/admin/default/index'
#rult = urllib.urlopen(url)
#jresult = simplejson.load(rult)
return dict(message=T('Hello World'),
number_of_problems=jresult["number_of_problems"],
questions=jresult["questions"])

JagChris

unread,
May 7, 2011, 4:17:41 PM5/7/11
to web2py-users
There is a table i building in another view to show the relevant json
data but i was starting to wonder if it is feasible

JagChris

unread,
May 7, 2011, 4:39:16 PM5/7/11
to web2py-users
i also have a mathworksheet view which i believe would output the
data, but i wantd to try the code what i have written for it :


{{extend 'layout.html'}}
<h1>This is the default/mathworksheet.html template</h1>
{{extend 'layout.html'}}

<h1>{{=message}}</h1>

<h2>Number of Problems: {{=number_of_problems}}</h2>
<form>
<table border="1" cellpadding="5" cellspacing="2">
<tr>
<th width="8">No.</th>
<th width="32">Question</th>
<th>Answer</th>
</tr>
{{i = 0}}
{{while i < number_of_problems:}}<tr><td>{{=i+1}}</
td><td>{{=questions[i][0]}} {{=questions[i][2]}} {{=questions[i][1]}}</
td><td><input name="answer_{{=i+1}}"/></td></tr>{{i = i + 1}}{{pass}}
<tfoot>
<tr><td colspan="2" align="right"><input
type="submit" value="Submit Answers"/></td></tr>
</tfoot>
</table>
</form>

Anthony

unread,
May 7, 2011, 4:56:11 PM5/7/11
to web...@googlegroups.com
On Saturday, May 7, 2011 4:16:03 PM UTC-4, JagChris wrote:

def index():

    if request.vars :
        quest_types = request.vars.typeofques
        quest_num   = request.vars.numofques
        RetURL="http://mathmakerapi.appspot.com/cs34wmathmaker/default/
worksheet.json?numberques=
"+quest_num+"&questype="+quest_types
 
Is the above a URL in your app (or another web2py app on the server)? If so, use the URL() function to create this URL (you can pass the vars as a dictionary).
 
        json_data = urllib.urlopen(RetURL)
        jsonresult = simplejson.load(json_data)
        session.jresult = jsonresult
        #redirect(URL('mathworksheet'))
        redirect(URL(r=request,f='mathworksheet',args=jresult))
 
I think that should be args=jsonresult or args=session.jresult (jresult doesn't appear to be defined anywhere). However, I'm not sure about passing a (potentially) long JSON string as a URL arg. Instead, since you're already storing jsonresult in the session, just have your mathworksheet function pull it out of the session. Or better yet, have your form post to the mathworksheet URL, and move the logic that generates the JSON to that function (or, if you want the form to post to the index function, then just pass the request.vars to the matheworksheet function via the vars argument to the URL function).
 
Also, why aren't you using a web2py form here? web2py forms will enable you to do validation and protect against double form submission and cross-site request forgery attacks.
 

        return dict(passedin=jsonresult)
 
The above line will never get called because the previous line is a redirect, and the function will exit upon redirect.
 
Before proceeding, I would recommend spending some time reading the book (http://web2py.com/book), particularly chapters 3, 4, and 7.
 
Anthony

Plumo

unread,
May 7, 2011, 6:33:59 PM5/7/11
to web...@googlegroups.com
Will depend on your use case whether you should redirect or just change view.
You can change the view with:
response.view = 'default/other.html'

pbreit

unread,
May 7, 2011, 8:07:04 PM5/7/11
to web...@googlegroups.com
You don't want "redirect" and "return" in the same block since both end the function.

A typical form handling plus redirect pattern is shown here:
Reply all
Reply to author
Forward
0 new messages