我正在尝试创建一个过滤器,如下图所示(来自Fiverr),它是选择与选项作为输入的组合,
我能够创建小部件并选择一个选项,并在提交时发送其子输入值,但在加载后,值消失了,没有选择任何选项,因为单选选择值不是由字段指定的,所以我需要直接从request.GET中获取它,但我无法将reqeust传递给小部件,我在Filter类中看到它,而不是在小部件中。
我的自定义筛选器:`class PriceFilter(Widget):template_name = 'widgets/PriceFilter.html'
def __init__(self, attrs=None, min=0, max=5000, *args, **kwargs):
self.min = min
self.max = max
# print(self.request)
print(kwargs)
super(PriceFilter, self).__init__()
def get_context(self, name, value, attrs=None, *args, **kwargs):
context = super().get_context(name, value, attrs)
context['min'] = self.min
context['max'] = self.max
print(self.request)
print(kwargs)
return context`
我的小工具模板`
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickableInside"
data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
Clickable inside
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableInside">
<div class="form-check">
<input class="form-check-input" type="radio" name="{{widget.name}}__selector"
id="{{widget.name}}__selector" #if request.GET.price__selector==1 then ceck this#>
<label class="form-check-label" for="{{widget.name}}__selector">
Default radio
</label>
<div class="input-filters">
<input type="hidden" name="{{widget.name}}__lt" id="id_{{widget.name}}__lt"
value="{{widget.value|default_if_none:''}}">
</div>
</div>
</ul>
</div>
”
我的过滤器
`class ListFilter(django_filters.FilterSet):price = django_filters.NumberFilter(widget=PriceFilter())
class Meta:
model = MainModel
fields = {
'name':['contains'],'price':[]
}
def __init__(self, *args, **kwargs):
print('kwgs')
# print(args[0])
self.request = kwargs.pop('request', None)
super(ListFilter, self).__init__(*args, **kwargs)
self.filters["price"].request = args[0]
”
1条答案
按热度按时间2wnc66cl1#
我可以通过在FilterModel的init函数上指定对小部件的请求来解决这个问题