我遇到了一个大问题。我有以下表格
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
)因此,基本上用户可以在登录时创建帖子。我想在不重新加载整个页面的情况下添加对帖子的评论功能。在这种情况下,我认为我必须使用ajax函数。
我不太理解“引用”的工作方式。我认为当我插入一个新的文本时,我必须指明来自哪个帖子,但正如我所说的,我感到困惑。您能简要解释一下如何将这两个表关联起来吗?因为我必须编写一个python函数,在其中我必须指定要在特定帖子上显示的评论。
<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>如您所见,我使用控制器中的python函数检索了视图上的帖子的所有数据。我想得到文本区的输入,把它放在数据库中,然后以某种方式显示在这篇文章上。
https://wwu39.pythonanywhere.com/prostudy
我的网站已经在booted上启动了。我遇到的问题是在论坛页面上。要访问论坛,您必须登录。别担心,我不会给你发垃圾邮件的。这是一个没有人会使用的小应用程序。去论坛,选择一个帖子,你就会看到问题。add comment按钮还没有做任何事情。我已经禁用了用户身份验证功能,以使您的操作更容易
发布于 2016-11-27 14:11:47
Web2py load component将是您问题的最佳解决方案,在web2py一书read LOAD中解释了完全相同的示例。如果你使用web2py组件,你不需要编写任何javascript代码,而且你可以很容易地在其他地方使用相同的组件。
看一下上面的例子。您只需对表定义、控制器和加载url进行少量更改。
将post列添加到comment模式,就像您所做的那样,并使post列可读且可写为false。因为您不想在表单中显示该列
db.define_table('comment_post',
Field('post','reference post', readable=False, writable=False),
Field('body','text'),
auth.signature
)然后,在嵌入load组件时,将post id作为vars传递。
{{=LOAD('comments','post.load', vars={'post_id': parent_post_id}, ajax=True)}}从父控制器传递parent_post_id,在您的例子中,我认为request.args(0)包含父id。
在控制器中,现在使用从加载url传递的post_id,并将其设置为字段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())希望这能有所帮助。
https://stackoverflow.com/questions/40800092
复制相似问题