我想知道如何连接2个模型以创建一个相互依赖的下拉列表。
在撰写本文时,这些是我的视频模型:
class AMLVideo(models.Model):
LANGUAGE = (
('LAN', 'Language'),
('FR', 'French'),
('EN', 'English'),
('HIN', 'Hindi'),
('SPA', 'Spanish'),
('GER', 'German'),
)
LEVEL = (
('BEG', 'Beginner'),
('INT', 'Intermediary'),
('ADV', 'Advanced'),
)
CATEGORY = (
('ANI', 'Animal'),
('ENV', 'Environmental'),
('MOR', 'Moral'),
('FOLK', 'Folktales'),
('ADN', 'Adventure'),
('POE', 'Poems'),
('FUN', 'Funny'),
)
slug = models.SlugField(max_length=50, default='Enter-Slug-Here')
level = models.CharField(max_length=100, choices=LEVEL, default='level', blank=True)
language = models.CharField(max_length=100, choices=LANGUAGE, default='language', blank=True)
category = models.CharField(max_length=100, choices=CATEGORY, default='category', blank=True)
video = EmbedVideoField(verbose_name='Videos',
help_text='URL of Video')
def __str__(self):
return self.slug
class Meta:
verbose_name = "video"
verbose_name_plural = "videos"
这是已经使用iframe显示视频的视图
def home(request):
videos = AMLVideo.objects.all()
# Get category from filter
category = request.GET.get('category', '')
if category:
videos = videos.filter(
category__exact=category
)
# Get language from filter
language = request.GET.get('language', '')
if language:
videos = videos.filter(
language__exact=language
)
# Get level from filter
level = request.GET.get('level', '')
if level:
videos = videos.filter(
level__exact=level
)
videos = videos.order_by("-category", "-language", "-level")
context = {'videos': videos}
return render (request, 'home.html', context)
现在是下拉菜单,我不太确定如何使用模板上的模型或表单来显示它。
我希望,如果用户从下拉菜单中选择了某项内容,则显示的视频将基于用户选择的下拉菜单。
这可能吗?
答案 0 :(得分:1)
您可以使用可以在模板中呈现的Django Form处理此问题,并保留过滤器值以发送到视图。像这样:
forms.py
from django import forms
class AMLVideoFilterForm(forms.Form):
LANGUAGE = (
('LAN', 'Language'),
('FR', 'French'),
('EN', 'English'),
('HIN', 'Hindi'),
('SPA', 'Spanish'),
('GER', 'German'),
)
LEVEL = (
('BEG', 'Beginner'),
('INT', 'Intermediary'),
('ADV', 'Advanced'),
)
CATEGORY = (
('ANI', 'Animal'),
('ENV', 'Environmental'),
('MOR', 'Moral'),
('FOLK', 'Folktales'),
('ADN', 'Adventure'),
('POE', 'Poems'),
('FUN', 'Funny'),
)
language = forms.ChoiceField(choices=LANGUAGE, widget=forms.Select(attrs={'onchange' : "this.form.submit()"}))
level = forms.ChoiceField(choices=LEVEL, widget=forms.Select(attrs={'onchange' : "this.form.submit()"}))
category = forms.ChoiceField(choices=CATEGORY, widget=forms.Select(attrs={'onchange' : "this.form.submit()"}))
views.py
def home(request):
filter_form = AMLVideoFilterForm(request.GET)
videos = AMLVideo.objects.all()
# Get category from filter
category = filter_form.data.get('category')
if category:
videos = videos.filter(
category__exact=category
)
# Get language from filter
language = filter_form.data.get('language')
if language:
videos = videos.filter(
language__exact=language
)
# Get level from filter
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}
return render (request, 'home.html', context)
home.html
<body>
...
<form method="GET">
{{ filter_form }}
</form>
{% for video in videos %}
<p>Render your video here</p>
{% endfor %}
...
</body>
然后,每当您更改过滤器选择时,它将以GET方法提交表单。在视图中,您将必须在表单数据中获取过滤器以过滤数据。关于小部件,您必须添加onchange事件,以便每当用户更改下拉值时,它将提交表单以再次过滤数据。这是不使用JavaScript或jQuery的另一种处理方式。
希望有帮助!