utility_tags.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:Django-Web-Development-with-Python 作者: PacktPublishing 项目源码 文件源码
def add_to_query(context, *params_to_remove, **params_to_add):
    """ Renders a link with modified current query parameters """
    query_params = []
    # go through current query params..
    for key, value_list in context['request'].GET._iterlists():
        if not key in params_to_remove:
            # don't add key-value pairs which already exist in the query
            if key in params_to_add and unicode(params_to_add[key]) in value_list:
                params_to_add.pop(key)
            for value in value_list:
                query_params.append((key, value))
    # add the rest key-value pairs
    for key, value in params_to_add.items():
        query_params.append((key, value))
    # empty values will be removed
    query_string = context['request'].path
    if len(query_params):
        query_string += "?%s" % urllib.urlencode([
            (key, force_str(value)) for (key, value) in query_params if value
        ]).replace("&", "&")
    return query_string
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号