Fixes return_url for image attachment (#12721)

* fixes return_url for image attachment #12538

* simplified conditions

* handle nonetype error

* fixed request check

* Introduce htmx_table template tag for embedding HTMX-backed object tables

---------

Co-authored-by: jeremystretch <jstretch@netboxlabs.com>
This commit is contained in:
Abhimanyu Saharan
2023-06-01 00:52:37 +05:30
committed by GitHub
parent 3e77daff01
commit dbd3c6de24
4 changed files with 32 additions and 8 deletions

View File

@@ -0,0 +1,4 @@
<div class="card-body htmx-container table-responsive"
hx-get="{% url viewname %}{% if url_params %}?{{ url_params.urlencode }}{% endif %}"
hx-trigger="load"
></div>

View File

@@ -1,4 +1,5 @@
from django import template
from django.http import QueryDict
__all__ = (
'badge',
@@ -74,3 +75,22 @@ def checkmark(value, show_false=True, true='Yes', false='No'):
'true_label': true,
'false_label': false,
}
@register.inclusion_tag('builtins/htmx_table.html', takes_context=True)
def htmx_table(context, viewname, return_url=None, **kwargs):
"""
Embed an object list table retrieved using HTMX. Any extra keyword arguments are passed as URL query parameters.
Args:
context: The current request context
viewname: The name of the view to use for the HTMX request (e.g. `dcim:site_list`)
return_url: The URL to pass as the `return_url`. If not provided, the current request's path will be used.
"""
url_params = QueryDict(mutable=True)
url_params.update(kwargs)
url_params['return_url'] = return_url or context['request'].path
return {
'viewname': viewname,
'url_params': url_params,
}