Django redirect using reverse() to a URL that relies on query strings

前端 未结 4 1794
Happy的楠姐
Happy的楠姐 2021-01-04 07:05

I\'m writing a django application with a URL like \'http://localhost/entity/id/?overlay=other_id\'. Where id is the primary key of the particular entity and overlay is an o

4条回答
  •  醉梦人生
    2021-01-04 07:49

    You can use a Django QueryDict object:

    from django.http import QueryDict
    
    # from scratch
    qdict = QueryDict('',mutable=True)
    
    # starting with our existing query params to pass along
    qdict = request.GET.copy()
    
    # put in new values via regular dict
    qdict.update({'foo':'bar'})
    
    # put it together
    full_url = reversed_url + '?' + qdict.urlencode()
    

    And of course you could write a convenience method for it similar to the previous answer.

提交回复
热议问题