我找到了这个项目https://github.com/chhantyal/taggit-selectize,它似乎可以满足我的要求,但是示例应用程序并不完整,所以我不知道如何使用它。我基本上想要的是用户将能够为帖子写自己的标签,并且如果他们键入以前被其他人使用过的标签,它将显示在自动完成中。我还想要一个搜索功能,该功能将以相同的方式使用自动完成功能。我确定它不是很复杂,但是自述文件仅说明了如何安装以及某些含义,并且我正在寻找一个使自动完成功能生效的非常小的工作示例。
先谢谢了。
答案 0 :(得分:2)
我有一个非常具体的用例。我希望我的示例不会使事情复杂化(我向Taggit模型添加了额外的字段)。请注意,根据此issue,您可能需要在HTML中加载CSS和JS。我正在使用Django Crispy Forms。
在我的应用程序设置中:
TAGGIT_TAGS_FROM_STRING = "taggit_selectize.utils.parse_tags"
TAGGIT_STRING_FROM_TAGS = "taggit_selectize.utils.join_tags"
TAGGIT_SELECTIZE_THROUGH = "jobsboard.models.SkillTags"
TAGGIT_CASE_INSENSITIVE = True
TAGGIT_SELECTIZE = {
"MINIMUM_QUERY_LENGTH": 2,
"RECOMMENDATION_LIMIT": 10,
"CSS_FILENAMES": ("taggit_selectize/css/selectize.django.css",),
"JS_FILENAMES": ("taggit_selectize/js/selectize.js",),
"DIACRITICS": True,
"CREATE": False,
"PERSIST": True,
"OPEN_ON_FOCUS": True,
"HIDE_SELECTED": True,
"CLOSE_AFTER_SELECT": False,
"LOAD_THROTTLE": 300,
"PRELOAD": False,
"ADD_PRECEDENCE": False,
"SELECT_ON_TAB": False,
"REMOVE_BUTTON": True,
"RESTORE_ON_BACKSPACE": False,
"DRAG_DROP": False,
"DELIMITER": ",",
}
在我的jobboard / models.py中:
from taggit.models import TagBase, GenericTaggedItemBase
from taggit_selectize.managers import TaggableManager
class SkillTags(TagBase):
LANGUAGE = "la"
STATISTICS = "st"
CODING = "co"
DISCIPLINE = "di"
TYPES = (
(LANGUAGE, "language"),
(STATISTICS, "statistics"),
(CODING, "coding"),
(DISCIPLINE, "discipline"),
)
type = models.CharField(choices=TYPES, default=DISCIPLINE, max_length=2)
creator = models.ForeignKey(User, null=True)
class TaggedModel(GenericTaggedItemBase):
tag = models.ForeignKey(SkillTags, related_name="%(app_label)s_%(class)s_items")
class Job(TimeStampedModel):
tags = TaggableManager(_("skillset required"), blank=True, through=TaggedModel)
在我的网址中:
url(r'^hirer/new/$', NewJobView.as_view(), name='hirer_new_job')
在我的jobboard / views.py中:
class NewJobView(FormView):
template_name = 'jobsboard/new_edit_job.html'
form_class = NewAndEditJobForm
在我的jobboard / forms.py中:
class NewAndEditJobForm(ModelForm):
class Meta:
model = Job
fields = ('tags',)
对于我的jobboard / templates / jobsboard / new_edit_job.html:
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
{{ block.super }}
{% crispy form %}
{% endblock content %}
对于我的template / base.html:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
{% block css %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Your stuff: Third-party CSS libraries go here -->
{% endblock %}
{% block extrahead %}
{% endblock %}
</head>
<body>
{% block content %}
{% endblock content %}
{% block javascript %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
{% endblock javascript %}
</body>
</html>
答案 1 :(得分:0)
我将尝试给出一个尽可能精简的示例:
app / models.py:
from django.db import models
from taggit_selectize.managers import TaggableManager
class Post(models.Model):
name = models.CharField(max_length=100)
tags = TaggableManager()
app / views.py:
from app.models import Post
from django.views.generic import UpdateView
class PostEdit(UpdateView):
model = Post
fields = ['name', 'tags' ]
success_url = '/'
app / templates / app / post_form.html:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Speichern">
</form>
{% endblock %}
{% block css %}
{{ block.super }}
<link href="{% static "taggit_selectize/css/selectize.django.css" %}" type="text/css" media="all" rel="stylesheet"/>
{% endblock %}
{% block javascript %}
{{ block.super }}
<script src="{% static "taggit_selectize/js/selectize.js" %}"></script>
{% endblock %}
和一个template / base.html:
{% load static %}<!DOCTYPE html>
<html lang="en">
<head>
<script src="{% static 'vendor/js/jquery-3.3.1.min.js' %}"></script>
<!-- IMPORTANT: get jquery loaded ASAP. if you load it too late you get JS errors from the taggit-selectize. -->
{% block css %}
{% endblock %}
</head>
<body>
{% block content %}
{% endblock %}
{% block javascript %}
{% endblock %}
</body>
</html>
重要提示:直到将jquery
加载项移动到标题中之前,我一直遇到相同的问题。