Django - 简单的自定义模板标签示例 [英] Django - Simple custom template tag example
问题描述
我有用户、视频、主题、标准和评分
- 视频有一个主题
- 一个主题有条件
- 用户可以为给定的主题创建视频
- 用户可以根据针对相关主题的每个标准对视频进行评分.
你可以看到我的原帖 Django - Rating Model Example DetailView Template获取所用模型的详细信息
我已经扩展了一个基于 video 模型的 DetailView
模板来放置所选视频的评分列表em> 对于给定的 user 作为额外的上下文.
class VideoFileDetailView(DetailView):模型 = 视频文件def get_context_data(self, **kwargs):context = super(VideoFileDetailView, self).get_context_data(**kwargs)context['rates'] = VideoRate.objects.filter(video=self.object, user=self.request.user)返回上下文
在DetailView
指向的模板中,我想列出视频的标准,以及每个标准显示用户的当前评分值.
<ul>{% for crit in videofile.topic.crits.all %}<li>{% for rate in rate %}{% if rate.crit.id == crit.id %}{{rate.rate}}{% 万一 %}{% 结束为 %}<div class="rateit"data-rateit-value="{# 评分值 #}"data-rateit-ispreset="true"crit-id="{{ crit.id }}"></div>{{ 暴击 }}{% 结束为 %}
(rateit 是一个 jquery 插件,我用来绘制漂亮的星星评分控件)
实际上,我在第二个 for
中获得了我的评分值,但我确信有更好的方法来做到这一点.事实上,我仍然不确定我的模型是否正确.>
最后,我想将 {# The rating value #}
替换为当前暴击(在循环中)的 rate 的评级值.我该怎么做?
这是我的解决方案(基于自定义标签):
首先创建文件结构.进入需要标签的应用目录,并添加以下文件:
模板标签模板标签/__init__.py模板标签/video_tags.py
templatetags/video_tags.py 文件:
from django 导入模板注册 = 模板.图书馆()@register.simple_tagdef get_rate(暴击率,率):退货率.get(crit=crit).rate
模板部分,带有我们的标签调用:
{% 加载 video_tags %}<div id="评级"><ul>{% for crit in videofile.topic.crits.all %}<li><div class="rateit"data-rateit-value="{% get_rate 暴击率 %}"data-rateit-ispreset="true"crit-id="{{ crit.id }}"></div>{{ 暴击 }}{% 结束为 %}
I have users, videos, topics, criterias and ratings
- A video has a topic
- A topic has criterias
- A user can create a video for a given topic
- A user can rate a video on each criterias given for the concerned topic.
You can see my original post Django - Rating Model Example DetailView Template to get details on the model used
I have extended a DetailView
template based on the video model to put the list of ratings for the selected video for a given user as extra context.
class VideoFileDetailView(DetailView):
model = VideoFile
def get_context_data(self, **kwargs):
context = super(VideoFileDetailView, self).get_context_data(**kwargs)
context['rates'] = VideoRate.objects.filter(video=self.object, user=self.request.user)
return context
In the template pointed by the DetailView
, I'd like to list the criterias of the video, and for each criteria display the current rating value form the user.
<div id="rating">
<ul>
{% for crit in videofile.topic.crits.all %}
<li>
{% for rate in rates %}
{% if rate.crit.id == crit.id %}
{{ rate.rate }}
{% endif %}
{% endfor %}
<div class="rateit"
data-rateit-value="{# The rating value #}"
data-rateit-ispreset="true"
crit-id="{{ crit.id }}"></div>
{{ crit }}
</li>
{% endfor %}
</ul>
</div>
(rateit is a jquery plugin that I use to draw pretty stars rating controls)
Actually I get my rating values here within the 2nd for
but I'm sure there is a better way to do that. In fact, I'm still not sure about my model correctness.
Finally I'd like to replace {# The rating value #}
by the rating value from rate for the current crit (in the loop). How can I do that ?
Here is my solution (based on a custom tag):
Firstly create the file structure. Go into the app directory where the tag is needed, and add these files:
templatetags
templatetags/__init__.py
templatetags/video_tags.py
The templatetags/video_tags.py file:
from django import template
register = template.Library()
@register.simple_tag
def get_rate(crit, rates):
return rates.get(crit=crit).rate
The template part, with our tag call:
{% load video_tags %}
<div id="rating">
<ul>
{% for crit in videofile.topic.crits.all %}
<li>
<div class="rateit"
data-rateit-value="{% get_rate crit rates %}"
data-rateit-ispreset="true"
crit-id="{{ crit.id }}"></div>
{{ crit }}
</li>
{% endfor %}
</ul>
</div>
这篇关于Django - 简单的自定义模板标签示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!