Re: [tornado] Radio button onclick event to call an handler

575 views
Skip to first unread message

Matthew Ferguson

unread,
Aug 29, 2012, 12:54:58 PM8/29/12
to python-...@googlegroups.com
This isn't Tornado related, this is JavaScript related.  However, you caught me on a good day, so I'll bite.

Inline JavaScript is bad!  Bad bad bad bad bad.  Glancing over your code quickly, assuming it works at all, you only have the onclick event firing on the labels, not the radio buttons themselves.

Here's what should happen:

<label> should have the "for" attribute, which is the ID of the input that should be selected when clicking on the label

You should use jQuery to attach JavaScript to your radio buttons.  I put together an example for you (this is not the absolute best way to go about this)


On Aug 29, 2012, at 9:22 AM, Irene Bontà <irene...@gmail.com> wrote:

Hi all!
 
I have a three choice form. I want call an handler when someone click on a radio button. 

I have this html form:

<form name="form_user_{{ user.id }}" method="post" action="/networks/{{ net.id }}/rights">
              <tr>
                <td>
                  <div>
                    {{ escape(user.name) }}
                    <input type='hidden' name="id" value='{{ user.id }}'>
                  </div>
                </td>
                <td>

                  <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();">
                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="0">
                    None
                  </label>
                  <label class="radio inline" onClick="document.form_user_{{ user.id }}.submit()">
                    <input type="radio" name="optionsRadios" id="optionsRadios2" value="1">
                    Read
                  </label>
                  <label class="radio inline" onClick="document.form_user_{{ user.id }}.submit()">
                    <input type="radio" name="optionsRadios" id="optionsRadios2" value="4">
                    Read + Commands
                  </label>



                  {{ xsrf_form_html() }}
                </td>

              </tr>
            </form>

And i want call the handler (r"/networks/([0-9]+)/rights", NetworkRightsHandler) which is this:

class NetworkRightsHandler(BaseHandler):
    @tornado.web.authenticated
    def post(self, netid):
        usrid = self.get_argument("id")
        perm = self.get_argument("perm")

ecc ecc.....

My solution doesn't work. What I've to do? Thank you very much, but i'm going crazy....

Irene Bontà

unread,
Aug 30, 2012, 6:58:23 AM8/30/12
to python-...@googlegroups.com
Thank you for your example! It's been very useful... 

But now I've tried with this solution:

{% for user in users %}
            <form name="form_user_{{ user.id }}" method="post" action="/networks/{{ net.id }}/rights">
              <tr>
                <td>
                  <div>
                    {{ escape(user.name) }}
                    <input type='hidden' name="id" value='{{ user.id }}'>
                  </div>
                </td>
                <td>

                  <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();">
                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="0">
                    None
                  </label>
                  <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();">
                    <input type="radio" name="optionsRadios" id="optionsRadios2" value="1">
                    Read
                  </label>
                  <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();">
                    <input type="radio" name="optionsRadios" id="optionsRadios2" value="4">
                    Read + Commands
                  </label>



                  {{ xsrf_form_html() }}
                </td>

              </tr>
            </form>
            {% end %}

This solution seems work at the click, but now I have an error from Tornado:

Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/tornado/web.py", line 988, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/tornado/web.py", line 1739, in wrapper
    return method(self, *args, **kwargs)
  File "./wsn.py", line 455, in post
    perm = self.get_argument("perm")
  File "/usr/lib/python2.6/site-packages/tornado/web.py", line 298, in get_argument
    raise HTTPError(400, "Missing argument %s" % name)
HTTPError: HTTP 400: Bad Request (Missing argument perm)

The handler is:

class NetworkRightsHandler(BaseHandler):
    @tornado.web.authenticated
    def post(self, netid):
        usrid = self.get_argument("id")
        perm = self.get_argument("perm")

ecc ecc..........

Why is not passed to the handler??? Thank you very much!

Peter Bengtsson

unread,
Sep 5, 2012, 2:16:40 PM9/5/12
to python-...@googlegroups.com
Because 'perm' is not included when the form is submitted. 

Sharkbait

unread,
Sep 5, 2012, 7:17:27 PM9/5/12
to python-...@googlegroups.com
Yes ;) i solved! Thank you!

2012/9/5 Peter Bengtsson <pet...@gmail.com>



--
Irene Bontà
Reply all
Reply to author
Forward
0 new messages