Ranting + web2py

1st of April, Happy April Fool everyone! It’s not a good day to crack some jokes today. I have been rather demotivating lately. Probably has used up all my energy and motivations for the previous project. Although I have moved on to a whole new better team, i feel a bit left out seeing the rest of the team are so excited with the idea while I’m partially dead. Meanwhile, there is another good idea with another good friend of mine. We have talked about it long ago but we were to busy to even discuss about it. Now my mind is depleted of inspirations. Happening currently is another project that I am doing with my sister. Probably, just probably, i was demoralized. Somehow.

I have a good Final Year Project supervisor and therefore i should come up with a good project proposal. Intending to do some social-technopreneur stuffs but not having strong motivations currently.

At work, I’m not moving on fast enough. It’s a total waste of time sitting here, uncertain of what to do and how to do. Self-learning is challenging indeed. But I am glad to have a nice company and colleagues.

Well sorry for the ranting.
Just an update on where am I stuck at web2py. Mapping of URL.

I intend to write a function in default.py with an argument. Not really sure what “argument” is but it seems like something one level down after function. Then, from View, i can pass the variables into the argument. Using “if” and “else”, i can selectively send the correct serial command.
so basically, it is something like, if i click on this image/link, it will send variable=”1″ to the controller. So at the controller, i can check if the receive variable is “1”, it will send this serial command. Else, it will send another one.

Here is part of the codes in default.py

test.lightstate="0"
if test.lightstate == "1":
serialport.write("\x03O!0101\x0D")
else:
serialport.write("\x03O!0100\x0D")
return dict(lightstate)

in test.html

<a href = "{{=URL('test',args =[lightstate], vars={1})}}"><img src ="/Comfort2/static/images/on.jpg" width ="75" height="75"</a>

the error that i get:

<type 'exceptions.NameError'> global name 'lightstate' is not defined

Okay, so how should i define that?

5 comments

  1. I highly recommend asking questions on the Google Group: https://groups.google.com/forum/?fromgroups=#!forum/web2py. Also, check these sections of the book: http://web2py.com/books/default/chapter/29/04#Dispatching and http://web2py.com/books/default/chapter/29/04#URL.

    In “args = [lightstate]”, lightstate is a variable (which has not been defined). Instead, you want to specify a string literal: args = [‘lightstate’].

    Note, vars={1} will also yield an error. The vars argument to URL() must be a dictionary, but {1} is actually a set object in Python. The vars argument to URL() is used to build a query string of name/value pairs (e.g., vars=dict(a=1, b=2) adds the following query string to the URL: ?a=1&b=2).

    If you do URL(‘test’, args=[‘lightstate’], vars=dict(state=1)), you will get /yourapp/default/test/lightstate?state=1, and when that URL is requested, request.args(0) will have the value “lightstate” and request.vars.state will have the value 1.

    I suspect instead you want to do URL(‘test’, args=[‘lightstate’, 1]), which will yield /yourapp/default/test/lightstate/1, in which case, request.args(0) will have the value “lightstate”, and request.args(1) will have the value 1.

    1. Hello Anthony, Thank you for your explanation. To be frank, I don’t really understand the definition of “args” and “vars”, and what are they used for. I have read through those chapters in the book but without examples, it’s quite difficult for me to digest i must say. :s
      i have just asked a question in the googlegroup… https://groups.google.com/forum/?fromgroups=#!topic/web2py/ICo5-yFgxmc
      hmmm…. what is the difference between URL(‘test’, args=[‘lightstate’], vars=dict(state=1)) and URL(‘test’, args=[‘lightstate’, 1])? As in, how do they function?
      i’m not sure which one i should use for my program.

      1. No problem. Looks like you’ve gotten some help on the Google Group. Happy to help more there.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s