request.build_absolute_uri() returns me url/path/?q1=v1&q2=v2... BUT, I need the same absolute uri without query params q1=v1&q2=v2
The build_absolute_uri method has an optional location. If no location is provided, it uses get_full_path() which includes the querystring. You can pass request.path (which doesn't include the querystring) as the location.
request.build_absolute_uri(request.path)
Expanding, if you want to do it from within a template, this is how to do it via writing a simple tag:
settings.py, add the following to TEMPLATE['OPTIONS']:'libraries': {
'common_extras': 'your_project.templatetags.common_extras',
},
your_project/templatetags/common_extras.py and add the following:from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def clean_url(context):
request = context['request']
return request.build_absolute_uri(request.path)
{% load common_extras %}
...
<meta property="og:url" content="{% clean_url %}">
...
<meta property="twitter:url" content="{% clean_url %}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With