我有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>
有人熟悉吗?
答案 0 :(得分:1)
如果您未选择任何过滤器,则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'),
]