Is it mandatory to add new html page for every new function ?

134 views
Skip to first unread message

kranthi aeronaut

unread,
Sep 6, 2013, 11:16:46 AM9/6/13
to web...@googlegroups.com
hii all , i am new to web2py , it seems that in web2py for every function we declare in default.py page , we should also create a new html page with same name of function , is this mandatory or is there any other way to implement the logic ??

Annet

unread,
Sep 6, 2013, 11:39:49 AM9/6/13
to web...@googlegroups.com
Hi,

In your functions you can set response.view='some_view.html'
and then have multiple functions use 'some_view.html'


Kind regards,

Annet

Anthony

unread,
Sep 6, 2013, 11:41:08 AM9/6/13
to web...@googlegroups.com
You can use the generic.html view, though it has limitations regarding how it will display your data. If you have a number of functions that all need to use the same view, you can simply specify the view explicitly:

def some_function():
    response
.view = 'default/my_common_view.html'

Or in a model or at the top of the controller:

if request.function in ['myfun1', 'myfun2', 'myfun3', ...]:
    response
.view = 'default/my_common_view.html'

Anthony

dhmorgan

unread,
Sep 6, 2013, 12:02:55 PM9/6/13
to web...@googlegroups.com
for any function, you can specify the template to be used to render the data you return; a couple of helpful bits from the book can be found at:

essentially, in your function, you can set the template using "response.view = 'controller_name/template_name.ext'"; alternately, you can combine the template and data in one with "return response.render('template_path',dictionary_name)"

kranthi aeronaut

unread,
Sep 7, 2013, 6:50:32 AM9/7/13
to web...@googlegroups.com
some please help me , i am using web2py for simple addition of 2 numbers application , i have created a function called "calc" in default.py and wrote the following code in it

def calc():
    answer=request.vars.a+request.vars.b
    return dict(answer)

i created a file called default/add.html & wrote following code in it:

<html>
<form action="calc" method="POST">
        <input name="a" required/><br>
        <input name="b" required/><br>
        <input type="submit"/>
</form>
</html>

again i created another file called default/calc.html & wrote the following code:

<html>
<h1>{{=answer}}</h1>
</html>

now when i enter 2 numbers in text field & click submit ,   i am getting lots of errors & unable to get the answer , is there anything wrong in code , please help me ....


--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/bCojfljja54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
"When FREEDOM matters,EVERY THING is returned to you"

-kr@nthi kum@r

Tim Richardson

unread,
Sep 7, 2013, 9:42:56 AM9/7/13
to web...@googlegroups.com


On Saturday, 7 September 2013 20:50:32 UTC+10, kranthi aeronaut wrote:
some please help me , i am using web2py for simple addition of 2 numbers application , i have created a function called "calc" in default.py and wrote the following code in it

def calc():
    answer=request.vars.a+request.vars.b
    return dict(answer)

i created a file called default/add.html & wrote following code in it:

<html>
<form action="calc" method="POST">
        <input name="a" required/><br>
        <input name="b" required/><br>
        <input type="submit"/>
</form>
</html>

again i created another file called default/calc.html & wrote the following code:

<html>
<h1>{{=answer}}</h1>
</html>

now when i enter 2 numbers in text field & click submit ,   i am getting lots of errors & unable to get the answer , is there anything wrong in code , please help me ....

Why don't you follow the examples in the book? (http://web2py.com/book) Start with the overview chapter. You are trying to submit data with a form. web2py handles this very easily, and it is all documented well in the book. The book will take you through several ways of doing this in web2py.
You should immediately follow the lead of the book (overview chapter under the heading "Say my name") which will show you how to get it working.



 

Ykä Marjanen

unread,
Sep 7, 2013, 12:59:00 PM9/7/13
to web...@googlegroups.com
You can redirect to any page you wish from a function. This way you don't have to create a page for the function and can decide what is the page you want to call.

example:

def no_page():
    redirect(URL('index'))

This is beneficial, if you want to have a function which handles the link data and then decides what page (view) to call.

Ykä

Anthony

unread,
Sep 7, 2013, 3:15:53 PM9/7/13
to web...@googlegroups.com


On Saturday, September 7, 2013 6:50:32 AM UTC-4, kranthi aeronaut wrote:
some please help me , i am using web2py for simple addition of 2 numbers application , i have created a function called "calc" in default.py and wrote the following code in it

def calc():
    answer=request.vars.a+request.vars.b
    return dict(answer)

i created a file called default/add.html & wrote following code in it:

<html>
<form action="calc" method="POST">

You action is a relative URL (i.e., just "calc"), so the browser simply adds that on to the URL of the current page -- so that form is getting posted to /yourapp/default/add/calc instead of /yourapp/default/calc. You should instead use the URL() function to generate the proper URL for you:

<form action="{{=URL('default', 'calc')}}" method="post">

Anthony

kranthi aeronaut

unread,
Sep 8, 2013, 1:39:39 AM9/8/13
to web...@googlegroups.com
thanx to all for your suggestions , anthony i have tried your method but i am getting error like this:

ValueError: dictionary update sequence element #0 has length 1; 2 is required
Reply all
Reply to author
Forward
0 new messages