Bootstrap modal for insert, update and delete

602 views
Skip to first unread message

M.C. Botha

unread,
Jan 18, 2015, 11:58:55 PM1/18/15
to
I would like to use a bootstrap modal to create new and update existing records using a bootstap modal.
It works for new records but I cannot get it to work for updating existing ones and thus not to delete either.

What am I doing wrong in the post.load views and in the comments.py.get_comment code?

Can someone please help?

models/tables.py
db.define_table('comment_post',
    Field('title','string',label='Comment title', requires=IS_NOT_EMPTY()),
    Field('body','text',label='Your comment', requires=IS_NOT_EMPTY()),
    auth.signature)

controllers/default.py
def index():
    return dict()

views/default/index.html
{{extend 'layout.html'}}

<h3>Comments</h3>
{{=LOAD('comments','post.load',ajax=True)}}

controllers/comments.py
@auth.requires_login()
def post():
    return dict(form=SQLFORM(db.comment_post, formstyle='bootstrap3_stacked').process(),
                comments=db(db.comment_post).select(orderby=~db.comment_post.id, limitby=(0, 3)))

@auth.requires_login()
def get_comment():
    r = db.auth_user(request.vars.id) or redirect(URL('index'))
    form = SQLFORM(db.comment_post, r)
    return form

views/comments/post.load
{{for post in comments:}}
    <div class="post">{{=post.title}}: on <small>{{=post.created_on}}</small> {{=post.created_by.first_name}}
        <small>said:</small> "<span class="post_body">{{=post.body}}</span>"
        {{=A('Edit', callback=URL('welcome', 'comments', 'get_comment', vars={'id': post.id}), target="form_load")}}
    </div>
{{pass}}

<hr>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div id="form_load" class="modal-body">
        {{=form}}
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Massimo Di Pierro

unread,
Jan 22, 2015, 5:34:05 PM1/22/15
to web...@googlegroups.com
I think you are missing .process()

form = SQLFORM(db.comment_post, r).process()

Serg Syuzev

unread,
Jan 23, 2015, 6:32:15 AM1/23/15
to web...@googlegroups.com
Hello!
I think, You can use some jQuery "magic" for begin. 
In views/comments/post.load in line:
{{=A('Edit', callback=URL('welcome', 'comments', 'get_comment', vars={'id': post.id}), target="form_load")}}
for example, add before "callback":
_onclick="jQuery('#myModal').modal('show');",


M.C. Botha

unread,
Jan 28, 2015, 12:01:48 PM1/28/15
to web...@googlegroups.com
I have tried that but it does not reload the form.
Reply all
Reply to author
Forward
0 new messages