In a custom html form of SQLFORM in web2py, how to copy the request argument of the url into a form field input?

51 views
Skip to first unread message

Anusha Narayan

unread,
Jul 4, 2020, 1:26:58 AM7/4/20
to web2py-users

The code of my model db.py is:

db.define_table(
                'event_table',
                Field('event_name'),
                Field('organizer_name'),
                )

db.define_table('category',
                Field('Category_name',requires=IS_NOT_EMPTY()),
                Field('Event_id',db.event_table)
                )

The controller looks like:

def category():

    form=SQLFORM(db.category)
    if form.process().accepted:
        response.flash='form has accepted'
    else:
        response.flash='form has errors'
    return dict()

The code for the view (default/category.html) is:

{{extend 'layout.html'}}



  <form action="#" enctype="multipart/form-data" method="post">
<ul>
    
  <li>category name: <input name="Category_name" value="{{=request.args(0)}}"/></li>
  <li>event_id type: <input name="Event_id" ></li>
</ul>
    <input type="submit" value="Submit" />
 </form>

(Here I am trying to pass the event id through the request argument of the URL. But when I submit the form, it says "form has errors". Why is that happening? I am not able to understand.?

AGRogers

unread,
Jul 6, 2020, 11:26:22 AM7/6/20
to web...@googlegroups.com
Do you mean you want to set the EVENT_ID so that it's already selected when the form opens?

Or are you saying that after you select the Event and submit the form you get an error? 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/38a3a4ba-7887-415a-8409-f5f28170d178o%40googlegroups.com.

Anusha Narayan

unread,
Jul 7, 2020, 2:37:03 PM7/7/20
to web...@googlegroups.com
Yeah! The first question, is what I am talking about. 

AGRogers

unread,
Jul 7, 2020, 8:33:30 PM7/7/20
to web...@googlegroups.com
OK. After you declare the form but before you process it you can set the form.vars.FieldName property. I think that might help. 

Jim S

unread,
Jul 8, 2020, 9:33:20 AM7/8/20
to web2py-users
Here is how I'd do it:

Controller:
def category():
    db
.category.name.default = request.args(0)
    form
=SQLFORM(db.category)
   
return dict(form=form)

Template:
{{extend 'layout.html'}}
{{=form}}

There are a couple of issues in your example

1.  You're using SQLFORM to create a form, but then you aren't doing anything with it.  You need to pass it to your template for it to be useful.
2.  The else portion of the if form.process().accepted is executed anytime you call the controller and haven't submitted the form.  Therefore, on your initial display of the form, your error message will display.

I hope this helps.  

-Jim
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Anusha Narayan

unread,
Jul 8, 2020, 11:16:56 AM7/8/20
to web...@googlegroups.com
Thankyou , this surely helps. 
But what if we want to custom our form using html and css? 

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/119b2c41-ae90-4684-aaea-3ebc5f6fde53o%40googlegroups.com.

Jim S

unread,
Jul 8, 2020, 11:26:47 AM7/8/20
to web2py-users
Then, in your template:

{{extend 'layout.html'}}
{{=form.custom.begin}}
   
<ul>
       
<li>{{=form.custom.label.Category_name}}: {{=form.custom.widget.Category_name}}</li>
       
<li>{{=form.custom.label.Event_id}}: {{=form.custom.widget.Event_id}}</li>
   
</ul>
   
{{=form.custom.submit}}
{{=form.custom.end}}


-Jim

AGRogers

unread,
Jul 8, 2020, 11:52:40 PM7/8/20
to web...@googlegroups.com
>>>  db.category.name.default = request.args(0)  
 That's a good idea. I forgot that you can just change that before building the form.
Thanks


To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/119b2c41-ae90-4684-aaea-3ebc5f6fde53o%40googlegroups.com.

Anusha Narayan

unread,
Jul 9, 2020, 5:06:34 AM7/9/20
to web...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages