Skip to main content

Django 模型的李克特字段

项目描述

Django 模型的李克特字段。用于添加星级评分功能。

<图> https://github.com/kelvinwong-ca/django-likert-field/raw/master/docs/images/add_form_rendered.jpg <图说明>

使用 jQuery [ 1 ]的 Bootstrap-star-rating 插件渲染

</figcaption> </figure>

为什么要用这个?

Django-likert-field 有以下好处:

  • 只是模型的简单字段类型(仅此而已)

  • 当您只需要一个字段时,不会让您添加一个充满东西的新表

  • 包括用于 Font Awesome 和 Bootstrap/Glyphicons 半身人的有用且简单的星形渲染过滤器

  • 包括一个简单的 Django 小部件,它生成可供 jQuery 星级评分小部件使用的 HTML

安装

这个包需要 Django 1.4.19 或更高版本。它可以使用 Pip 以通常的方式安装:

pip install django-likert-field

然后将该应用添加到您的已安装应用列表中:

# settings.py
#
INSTALLED_APPS = (
    'likert_field',

    ...other apps...
)

而已。不需要'syncdb'。您现在可以将该字段附加到您的模型。

基本用法

以与常规模型字段相同的方式使用:

# models.py
#
from likert_field.models import LikertField
class PetShopSurvey(models.Model):
    i_like_snakes = LikertField()

在您的 add.html 模板中:

# add.html
#
<form method="post">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Save</button>
</form>

呈现类似于此的 HTML:

# Renders a widget
#
# jQuery star-rating widget should be able to grab by 'likert-field' class
#
<label for="id_i_like_snakes">I like snakes:</label>
<input id="id_i_like_snakes" type="text" name="i_like_snakes"
 class="likert-field" />

检索您的响应时,请使用提供的 Django 过滤器之一:

# detail.html
#
# assume 'survey' is the context object holding survey instance
#
{% load likert_fa_stars %}
{{ survey.i_like_snakes|fa_stars4 }}

这将为您选择的框架渲染星星(在本例中为 Font Awesome 4):

# Renders stars
#
# assuming one-star Likert item score
#
<i class='fa fa-star likert-star'></i> ... other stars maybe...

Django 模型中的 LikertField

默认情况下,您的 LikertField 具有以下设置:

  • 用户响应是可选的(空白=真)

  • score 是一个从 0 到 n 的整数

  • 最小值为零 (min_value=0)

  • 没有最大值(max_value=None)

  • “No answer”在数据库中存储为 NULL

LikertField 将您的 Likert 项目的分数存储为从零到 n 的简单整数。您可以根据需要设置一个 max_value,但默认情况下不设置一个。假设您的项目是李克特 5 分项目。

将字段放在模型上:

# models.py
#
from django.db import models
from likert_field.models import LikertField

class PetShopSurvey(models.Model):
    i_like_snakes = LikertField()

如果您需要回复,可以将“空白”设置为 False:

# models.py
#
from django.db import models
from likert_field.models import LikertField

class PetShopSurvey(models.Model):
    i_like_snakes = LikertField(blank=False)

如果您需要用户从 1 到 7 分(李克特 7 分项)。您可以设置最小值和最大值的组合,并将空白设置为 False 以强制响应:

# models.py
#
from django.db import models
from likert_field.models import LikertField

class PetShopSurvey(models.Model):
    i_like_snakes = LikertField(
        min_value=1,
        max_value=7,
        blank=False)

形式

这个包包括一个名为 LikertFormField 的表单域。它可用于创建 Django 表单:

# forms.py
#
from django.forms import Form
from likert_field.forms import LikertFormField

class SurveyForm(Form):
    i_like_snakes = LikertFormField()

这将呈现一个具有以下 HTML 的表单:

<p>
  <label for="id_i_like_snakes">I like snakes:</label>
  <input id="id_i_like_snakes" type="text" name="i_like_snakes" class="likert-field" />
</p>

小部件

还有一个名为 LikertTextField 的简单小部件。它本质上是一个 TextInput 小部件,它将一个类(“likert-field”)添加到生成的 HTML 输入中:

>>> from likert_field.widgets import LikertTextField
>>> w = LikertTextField()

>>> w.render('item_1', 3)
u'<input type="text" name="item_1" value="3" class="likert-field" />'

>>> w.render('item_1', None)
u'<input type="text" name="item_1" class="likert-field" />'

渲染您的李克特字段

一旦数据在模型中,您可以通过模板上下文以常规方式将模型实例传递给 Django 模板来呈现数据。进入模板后,您可以使用其中一个模板标签将整数数据呈现为一排星形。:

# in Django template detail.html
#
{% load likert_fa_stars %}
{{ survey.i_like_snakes|fa_stars4 }}

# It will render the following HTML
<i class='fa fa-star likert-star'></i>...etc...

一般的方案是通过适当的模板标签过滤模型字段。

引导星

<图> https://github.com/kelvinwong-ca/django-likert-field/raw/master/docs/images/bs_stars_color_style.png <图说明>

Mac Chrome 上的星星。

</figcaption> </figure>

Bootstrap 使用 Glyphicon halflings 作为字体图标。为 Bootstrap 设置了一个模板标签:

# in Django template detail.html
#
{% load likert_bs_stars %}
{{ survey.i_like_snakes|bs_stars3 }}

# It will render the following HTML
<i class='glyphicon glyphicon-star likert-star'></i>...etc...

Bootstrap 3 的两种星型是:

# A lit star
<i class='glyphicon glyphicon-star likert-star'></i>

# An unlit star
<i class='glyphicon glyphicon-star-empty likert-star'></i>

您可以使用“likert-star”类为星星添加其他样式:

/* Color the star red comrade */
.likert-star {
    color: #ff0000;
}

然后星星将呈现您想要的颜色。

<图> https://github.com/kelvinwong-ca/django-likert-field/raw/master/docs/images/bs_stars_red_style.png <图说明>

Mac Chrome 上的红色星星。

</figcaption> </figure>

字体真棒星星

Font Awesome 是一个流行的字体图标集。为它设置了一个模板标签:

# in Django template detail.html
#
{% load likert_fa_stars %}
{{ survey.i_like_snakes|fa_stars4 }}

# It will render the following HTML
<i class='fa fa-star likert-star'></i>...etc...

Font Awesome 4 的两种星型是:

# A lit star
<i class='fa fa-star likert-star'></i>

# An unlit star
<i class='fa fa-star-o likert-star'></i>

您可以使用“likert-star”类为星星添加其他样式:

/* Color the star Foundation 5 blue */
.likert-star {
    color: #008CBA;
}

然后星星将呈现您想要的颜色。

<图> https://github.com/kelvinwong-ca/django-likert-field/raw/master/docs/images/fa_stars_foundation_5_style.png <图说明>

Mac Chrome 上的蓝色星星。

</figcaption> </figure>

您可以使用标准方法将样式附加到点亮和未点亮的星星:

/* Gold stars wih outline */
.fa.fa-star.likert-star {
    color: #ffd76e;
    text-shadow:-1px -1px 0 #e1ba53,
                 1px -1px 0 #e1ba53,
                -1px  1px 0 #e1ba53,
                 1px  1px 0 #e1ba53;
    -webkit-text-stroke: 1px #e1ba53;
}
.fa.fa-star-o.likert-star {
    color: #c0c0c0;
}

然后明星们将采用这些风格。

<图> https://github.com/kelvinwong-ca/django-likert-field/raw/master/docs/images/fa_stars_deluxe_style.png <图说明>

Mac Chrome 上的金色星星。

</figcaption> </figure>

渲染 7 点李克特项目

渲染 7 点李克特(或 n 点李克特)很简单。将最大星数作为参数附加到过滤器:

{{ survey.i_like_snakes|bs_stars_3:7 }}

可用的过滤器

引导程序

对于引导程序 2 和 3:

{% load likert_bs_stars %}

# Bootstrap 2
{{ survey.i_like_snakes|bs_stars_2 }}

# Bootstrap 3
{{ survey.i_like_snakes|bs_stars_3 }}

字体真棒

对于 Font Awesome 3 和 4:

{% load likert_fa_stars %}

# Font Awesome 3
{{ survey.i_like_snakes|fa_stars3 }}

# Font Awesome 4
{{ survey.i_like_snakes|fa_stars4 }}

示例应用程序

如果您下载了 tarball,则会包含一个示例应用程序。你可以这样尝试:

$ pwd
/home/user/teststuff/django-likert-field
$ cd test_projects/django14
$ python manage.py syncdb
$ python manage.py runserver

Validating models...

0 errors found
Django version 1.4.19, using settings 'django14.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

故障排除

Django-likert-field 包含两个测试套件。一个用于字段,一个用于 Django 1.4.19 项目中字段的实现。

您可以通过下载 tarball 并在 setup.py 中运行“测试”来运行现场测试:

$ python setup.py test

您可以以类似的方式运行 Django 1.4.19 演示测试:

$ python setup.py test_demo

不用说,您需要安装 Django 1.4.19 或更高版本。

虫子!帮助!!

如果您在此软件中发现任何错误,请通过 Github 问题跟踪器[ 2 ]报告它们或发送电子邮件至code @ kelvinwong _ 任何严重的安全漏洞都应仅通过电子邮件报告。

谢谢

感谢您抽出宝贵时间评估此软件。我很高兴收到您对使用它的体验的反馈,我欢迎代码贡献和开发想法。

http://www.kelvinwong.ca/coders

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

django-likert-field-0.2.0.tar.gz (31.3 kB 查看哈希)

已上传 source