我有3个按钮,应该改变在页面上的视频的行为取决于所选择的下拉列表。但是用户从下拉列表中选择一个选项,视频就不合作了。
这是前端。当我选择一些例如语言的东西时,视频应该会改变,但它没有改变。

页面在http://localhost:8000/?language=EN&level=LEV&category=CAT上结束,然后分页

我尝试在视图上添加一个reverse_url,但仍然不能修复错误。
def home(request):
filter_form = AMLVideoFilterForm(request.GET)
videos = AMLVideo.objects.all()
category = filter_form.data.get('category')
if category:
videos = videos.filter(
category__exact=category
)
language = filter_form.data.get('language')
if language:
videos = videos.filter(
language__exact=language
)
level = filter_form.data.get('level')
if level:
videos = videos.filter(
level__exact=level
)
videos = videos.order_by("-category", "-language", "-level")
context = {'videos': videos, 'filter_form': filter_form, 'level': level, 'language': language, 'category': category,}
return render(request, 'home.html', context)forms.py为:
class AMLVideoFilterForm(forms.Form):
LANGUAGE = [
('LAN', 'Language'),
('EN', 'English'),
('FR', 'French'),
('HIN', 'Hindi'),
('SPA', 'Spanish'),
('GER', 'German'),
]
LEVEL = [
('LEV', 'Level'),
('BEG', 'Beginner'),
('INT', 'Intermediary'),
('ADV', 'Advanced'),
]
CATEGORY = [
('CAT', 'Category'),
('ADN', 'Adventure'),
('ANI', 'Animal'),
('ENV', 'Environmental'),
('MOR', 'Moral'),
('FOLK', 'Folktales'),
('POE', 'Poems'),
('FUN', 'Funny'),
]
language = forms.ChoiceField(
required=False,
choices=LANGUAGE,
widget=forms.Select(
attrs={
'onchange' : "this.form.submit()",
'class':'button waves-effect waves-light btn mt-10 mb-10 center-align'
}
)
)
level = forms.ChoiceField(
required=False,
choices=LEVEL,
widget=forms.Select(
attrs={'onchange' : "this.form.submit()",
'class':'button waves-effect waves-light btn mt-10 mb-10 center-align'
}
)
)
category = forms.ChoiceField(
required=False,
choices=CATEGORY,
widget=forms.Select(
attrs={'onchange' : "this.form.submit()",
'class':'button waves-effect waves-light btn mt-10 mb-10 center-align'
}
)
)这是在前端模板上
<section class="section-padding portfolio-container">
<div class="container center-align">
<div class="row">
<form method="GET">
{{ filter_form }}
</form>
</div>
</div>
</section>
<!-- VIDEOS -->
<section class="section-padding">
<div class="container">
<div class="row">
<div>
{% if videos %}
{% for v in videos %}
{% video v.video as my %}
<iframe width="{{ 380 }}" height="{{ 225 }}" src="{{ my.url }}"
frameborder="0" allowfullscreen></iframe>
{% endvideo %}
{% endfor %}
</div>
</div> <!-- Row -->
</div> <!-- container -->
</section>有人熟悉这个吗?
发布于 2019-09-25 03:06:47
如果您没有选择任何过滤器,那么CATEGORY和LEVEL的值应该为空,但有两个值CAT和LEV。这就是为什么当你过滤你的视频时,没有数据返回。
当您没有选择选择值时,尝试将选择值设置为空,如下所示更改表单:
class AMLVideoFilterForm(forms.Form):
LANGUAGE = [
('', 'Choose language'),
('EN', 'English'),
('FR', 'French'),
('HIN', 'Hindi'),
('SPA', 'Spanish'),
('GER', 'German'),
]
LEVEL = [
('', 'Choose level'),
('BEG', 'Beginner'),
('INT', 'Intermediary'),
('ADV', 'Advanced'),
]
CATEGORY = [
('', 'Choose category'),
('ADN', 'Adventure'),
('ANI', 'Animal'),
('ENV', 'Environmental'),
('MOR', 'Moral'),
('FOLK', 'Folktales'),
('POE', 'Poems'),
('FUN', 'Funny'),
]https://stackoverflow.com/questions/58084622
复制相似问题