如何获取url中传递的参数?

kiwipi 发布于 2012/08/27 09:02
阅读 1K+
收藏 0

我想实现回复用户发言的功能。“回复”带有超链接,链接中含有该条发言的id,跳转到新页面后,利用该id从数据库中搜索该条发言并在页面上显示。该页上还有一个回复的表单,我想在提交表单时,顺便把发言的id一起提交,这样好把发言和回复关联起来。目前已实现的有,显示发言,困难是,怎么在提交回复时,获取这个id。

def comment(request, page=1):
    if request.session.has_key('username'):
        username = request.session['username']
    after_range_num = 5
    bevor_range_num = 4
    try:
        page = int(request.GET.get("page", 1))
        if page < 1:
            page = 1
    except ValueError:
        page = 1
    comment_id = request.GET['cid']
    blog = db.Blog.find_one({'_id':ObjectId(comment_id)}) #to display the message in the comment page
    infolist = db.Comment.find({'_id': ObjectId(comment_id)}).sort("date", DESCENDING)
    info = []
    for i in infolist:
    info.append(i)
  
    paginator = Paginator(info, 3)
    try:
        articleList = paginator.page(page)
    except( EmptyPage,InvalidPage,PageNotAnInteger):
           articleList = paginator.page(1)
  
       #显示范围
    if page >= after_range_num:
       page_range = paginator.page_range[page-after_range_num:page+bevor_range_num]
    else:
       page_range = paginator.page_range[0:int(page)+bevor_range_num]
    return render_to_response("status/comment.html", {'articleList': articleList,
                                                       'info': info,
                                                       'username':username,
                                        'blog': blog,
                                                       'comment_id': comment_id
                                                        }
        )
    if request.method =='POST':
       if 'text' not in request.POST:
           return HttpResponseRedirect("/comment")
   
    comment_id = request.GET['cid']
   
       message = {"text": request.POST['text'],
                   "date": datetime.datetime.utcnow(),
                   #"username": username,
             "comment_id": comment_id}
       db.Comment.insert(message) # collection Comment is used to store users' comment of the message stored in collection Blog
       return HttpResponseRedirect("/comment?"+request.GET['cid'])

加载中
0
0
kiwipi
kiwipi
情况有变,我把参数加到Post的url中了。
返回顶部
顶部