how to redirect to a url with query string django

后端 未结 4 1120
星月不相逢
星月不相逢 2020-12-31 06:16

AoA, How can I goto a specific URL with parameters like if I have view

def search(request):

and in urls.py

^search/$ 
         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 06:50

    I know this question is a bit old, but someone will stumble upon this while searching redirect with query string, so here is my solution:

    import urllib
    from django.shortcuts import redirect
    
    def redirect_params(url, params=None):
        response = redirect(url)
        if params:
            query_string = urllib.urlencode(params)
            response['Location'] += '?' + query_string
        return response
    
    def your_view(request):
        your_params = {
            'item': 4
        }
        return redirect_params('search_view', your_params)
    

提交回复
热议问题