我尝试在管理员视图外设置django自动完成灯。我正在按照这个教程,但我没有运气,弄清楚什么是错的:
class TvShowModel(models.Model):
tvs_id = models.IntegerField(primary_key=True)
tvs_name = models.CharField(max_length=100)
tvs_name_br = models.CharField(max_length=100, blank=True,default="")
tvs_genre = models.CharField(max_length=100, blank=True,default="", null=True)
tvs_language = models.CharField(max_length=100, blank=True,default="", null=True)
tvs_status = models.CharField(max_length=100, null=True)
tvs_runtime = models.FloatField(blank=True,default="", null=True)
tvs_schedule = models.CharField(max_length=100, blank=True,default="", null=True)
tvs_rating = models.CharField(max_length=100, blank=True,default="", null=True)
tvs_timezone = models.CharField(max_length=100, blank=True,default="", null=True)
tvs_imdb_id = models.CharField(max_length=100, null=True, blank=True,default="")
tvs_img_m_url = models.CharField(max_length=100, null=True, blank=True,default="")
tvs_summary = models.TextField(max_length=100, null=True, blank=True,default="")
tvs_summary_br = models.TextField(max_length=100, null=True, blank=True,default="")
tvs_likes = models.FloatField()
def __str__(self):
return self.tvs_name
class TvAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
#if not self.request.user.is_authenticated():
# return Doctor.objects.none()
qs = TvShowModel.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
class TvShowForm(autocomplete.FutureModelForm):
class Meta:
model = TvShowModel
fields = ('tvs_name',)
widgets = {
'TvShowModel': autocomplete.ModelSelect2(url='select2_outside_admin')
}
class sss(UpdateView):
model = TvShowModel
form_class = TvShowForm
template_name = 'webapp/select2_outside_admin.html'
success_url = reverse_lazy('select2_outside_admin')
def get_object(self):
return TvShowModel.objects.first()
class TvShowForm(autocomplete.FutureModelForm):
class Meta:
model = TvShowModel
fields = ('tvs_name',)
widgets = { 'tvshow': autocomplete.ModelSelect2(url='select2_outside_admin', attrs={ 'data-html' : 'true' })}
#Tried this way also:
#widgets = {
#'TvShowModel': autocomplete.ModelSelect2(url='select2_outside_admin')
#}
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^showlist', views.showlist, name='showlist'),
url(r'^search', views.search, name='search'),
url(r'^sss',views.TvAutocomplete.as_view(),name='select2_outside_admin'),
]
{#% extends 'base.html' %#}
{# Don't forget that one ! #}
{% load static %}
<script src="{% static 'assets/css/select2.css' %}"></script>
{% block content %}
<div>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
</div>
{% endblock %}
{% block footer %}
<script src="{% static 'assets/js/select2.min.js' %}"></script>
<script src="{% static 'assets/js/select2.js' %}"></script>
<script src="{% static 'assets/js/search.js' %}"></script>
<script src="{% static 'sep/jquery-1.7.2.js' %}"></script>
<script src="{% static 'sep/autocomplete.init.js' %}"></script>
<script src="{% static 'sep/forward.js' %}"></script>
{{ form.media }}
{% endblock %}
如果我将url ^ sss设置为views.TvAutocomplete.as_view(),则响应为:
{“results”:[{“id”:1,“text”:“在圆顶下”},{“id”:2,“text”:“感兴趣的人”},{“id”: 3,“text”:“Bitten”},{“id”:4,“text”:“Arrow”},{“id”:5,“text”:“True Detective”},{“id”:6 ,“text”:“The 100”},{“id”:7,“text”:“Homeland”},{“id”:8,“text”:“Glee”},{“id”:9, “text”:“Revenge”},{“id”:10,“text”:“Grimm”}],“pagination”:{“more”:true}}
如果我将url ^ sss设置为views.sss.as_view(),则响应为(但该字段不起作用):