-
-
Save n0mn0m/168c9dc97d217ee633c4810ddb55b36b to your computer and use it in GitHub Desktop.
Django reverse with a querystring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import reverse | |
from django.utils.http import urlencode | |
def reverse_querystring(view, urlconf=None, args=None, kwargs=None, current_app=None, query_kwargs=None): | |
'''Custom reverse to handle query strings. | |
Usage: | |
reverse('app.views.my_view', kwargs={'pk': 123}, query_kwargs={'search': 'Bob'}) | |
''' | |
base_url = reverse(view, urlconf=urlconf, args=args, kwargs=kwargs, current_app=current_app) | |
if query_kwargs: | |
return f"{base_url}?{urlencode(query_kwargs)}" | |
return base_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment