Including a querystring in a django.core.urlresolvers reverse() call

后端 未结 5 800
一个人的身影
一个人的身影 2021-02-01 12:58

I\'m trying to reverse a named URL and include a querystring in it. Basically, I\'ve modified the login function, and I want to send ?next= in it.

Here\'s w

5条回答
  •  醉话见心
    2021-02-01 13:22

    I just made my own utility function like the one asked in the question:

    from django.utils.http import urlencode
    
    def my_reverse(viewname, kwargs=None, query_kwargs=None):
        """
        Custom reverse to add a query string after the url
        Example usage:
        url = my_reverse('my_test_url', kwargs={'pk': object.id}, query_kwargs={'next': reverse('home')})
        """
        url = reverse(viewname, kwargs=kwargs)
    
        if query_kwargs:
            return u'%s?%s' % (url, urlencode(query_kwargs))
    
        return url
    

提交回复
热议问题