TheMathNoob
TheMathNoob

Reputation: 317

Using the ajax function to store data in database (web2py framework)

I am having a big problem. I have the following tables

db.define_table('post',
                Field('user_email', default=auth.user.email if auth.user_id else None),
                Field('title', 'string', requires=IS_NOT_EMPTY()),
                Field('body', 'text', requires=IS_NOT_EMPTY()),
                Field('votes', 'integer', default=0, readable=False, writable=False),
                auth.signature
                )

db.define_table('comm',
                Field('post','reference post'),
                Field('body','text'),
                auth.signature
                )

So basically the user is able to create a post when he is logged in. I want to add the feature of adding comments on a post without reloading the whole page. In this case I would think I have to use the ajax function.

I don't quiet understand the way 'reference' works. I think that when I insert a new text I have to specify which post is coming from, but as I said, I am confused. Can u provide a brief explanation of how to relate the two tables? because I have to code a python function in which I have to specify the comments to be displayed on a specific post.

<div class="well">
    <h1>{{=post.title}}</h1>
    {{=post.body}}
</div>

{{if field_var_created_by == auth.user_id:}}
<a href={{=URL('edit_post',args=post.id)}} class="btn btn-danger" role="button">Edit Post</a>
{{pass}}

<form>
<div class="form-group">
  <label for="comment">Comment:</label>
  <textarea class="form-control" rows="5" id="comment"></textarea>
    <script>
        document.write('hello');
    </script>
</div>
</form>
<a href="" class="btn btn-success" role="button">Add Comments</a>

As you can see I retrieved all the data of a post on a view by using a python function in the controller. I want to get the input of text area, put it in the database and then somehow display it on this post.

https://wwu39.pythonanywhere.com/prostudy

I have my website booted up on pythonanywhere. The problem I am having is in the forum page. To access the forum you have to log in. Don't worry I am not going to spam you. This is a small app no one will use. Go to forum, select a post and you will see the problem. The add comment button doesn't do anything yet. I have disabled the user authentication feature to make things easier for you

Upvotes: 3

Views: 328

Answers (1)

Gaurav Vichare
Gaurav Vichare

Reputation: 1183

Web2py load component will be the best solution to your problem, exact same example is explained in web2py book, read LOAD. If you use web2py component, you don't need to write any javascript code, also you can easily use same component at other places.

Go through the above example. You have to do few changes in table definition, controller and load url.

Add post column to comment schema, just like you did and make post column readable and writable false. Because you don't want to show that column in form

db.define_table('comment_post',
                Field('post','reference post', readable=False, writable=False),
                Field('body','text'),
                auth.signature
                )

Then while embedding load component, pass post id as vars

{{=LOAD('comments','post.load', vars={'post_id': parent_post_id}, ajax=True)}}

Pass parent_post_id from parent controller, in your case i think request.args(0) contains parent id.

In controller, now use post_id passed from load url and set it to field post

def post():
    db.comment_post.post.default = request.vars.post_id
    return dict(form=SQLFORM(db.comment_post).process(),
                comments=db(db.comment_post).select())

Hope this helps.

Upvotes: 2

Related Questions