在管理员视图外使用 django-autocomplete-light
using django-autocomplete-light outside admin view
并尝试在一个简单的 html 页面中使用该小部件,但它不起作用并且没有 JavaScript 或 Python 错误。
这是我的代码:
# models.py
class Doctor(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200, null=True, blank=True, default='')
def __unicode__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=200)
sample = models.OneToOneField(Sample, null=True, blank=True)
doctor = models.ForeignKey(Doctor, null=True, blank=True, default=None)
def __unicode__(self):
return self.name
# views.py
class DoctorAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return Doctor.objects.none()
qs = Doctor.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
class DocForm(autocomplete.FutureModelForm):
class Meta:
model = Doctor
fields = ('name',)
widgets = {
'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
}
class Index(generic.UpdateView):
model = Doctor
form_class = DocForm
template_name = 'index.html'
success_url = reverse_lazy('index')
def get_object(self):
return Doctor.objects.first()
# index.html
<html>
<head>
<script type="text/javascript" src="/static/admin/js/jquery.js"></script>
</head>
<body>
Welcome
<div>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
</div>
</body>
</html>
我只想使用他们漂亮的 select2 小部件列出医生姓名和医生 ID 作为自动完成中的值。我可以在没有 django-autocomplete-light 的情况下做到这一点,但我明确想学习如何使用他们看起来更好的小部件来做到这一点。
有人可以指导我让它工作吗?
更新
您应该在 Patient
模型而不是 Doctor
模型中覆盖 'doctor'
属性的小部件:
class DocForm(autocomplete.FutureModelForm):
class Meta:
model = Patient
fields = ('doctor',) # or ('__all__')
widgets = {
'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
}
此外,请确保访问 reverse('doc-autocomplete')
url 为登录用户提供预期的结果,如 autocomplete tutorial.
中所述
在尝试解决类似问题时,我发现我的模板中没有包含 {{ form.media }}
。
在docs中有描述:
{% block footer %}
<script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script>
{{ form.media }}
{% endblock %}
我知道了:
class PatForm(autocomplete.FutureModelForm):
class Meta:
model = Patient
fields = ('doctor',)
widgets = {
'doctor': autocomplete.ModelSelect2Multiple(url='doc-autocomplete'),
}
class Index(generic.UpdateView):
model = Patient
form_class = PatForm
template_name = 'index.html'
success_url = reverse_lazy('index')
def get_object(self):
return Doctor.objects.first()
产生:
如果将 ModelSelect2Multiple 替换为 ModelSelect2
然后你会得到:
并尝试在一个简单的 html 页面中使用该小部件,但它不起作用并且没有 JavaScript 或 Python 错误。 这是我的代码:
# models.py
class Doctor(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200, null=True, blank=True, default='')
def __unicode__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=200)
sample = models.OneToOneField(Sample, null=True, blank=True)
doctor = models.ForeignKey(Doctor, null=True, blank=True, default=None)
def __unicode__(self):
return self.name
# views.py
class DoctorAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return Doctor.objects.none()
qs = Doctor.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs
class DocForm(autocomplete.FutureModelForm):
class Meta:
model = Doctor
fields = ('name',)
widgets = {
'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
}
class Index(generic.UpdateView):
model = Doctor
form_class = DocForm
template_name = 'index.html'
success_url = reverse_lazy('index')
def get_object(self):
return Doctor.objects.first()
# index.html
<html>
<head>
<script type="text/javascript" src="/static/admin/js/jquery.js"></script>
</head>
<body>
Welcome
<div>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
</div>
</body>
</html>
我只想使用他们漂亮的 select2 小部件列出医生姓名和医生 ID 作为自动完成中的值。我可以在没有 django-autocomplete-light 的情况下做到这一点,但我明确想学习如何使用他们看起来更好的小部件来做到这一点。 有人可以指导我让它工作吗?
更新
您应该在 Patient
模型而不是 Doctor
模型中覆盖 'doctor'
属性的小部件:
class DocForm(autocomplete.FutureModelForm):
class Meta:
model = Patient
fields = ('doctor',) # or ('__all__')
widgets = {
'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
}
此外,请确保访问 reverse('doc-autocomplete')
url 为登录用户提供预期的结果,如 autocomplete tutorial.
在尝试解决类似问题时,我发现我的模板中没有包含 {{ form.media }}
。
在docs中有描述:
{% block footer %} <script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script> {{ form.media }} {% endblock %}
我知道了:
class PatForm(autocomplete.FutureModelForm):
class Meta:
model = Patient
fields = ('doctor',)
widgets = {
'doctor': autocomplete.ModelSelect2Multiple(url='doc-autocomplete'),
}
class Index(generic.UpdateView):
model = Patient
form_class = PatForm
template_name = 'index.html'
success_url = reverse_lazy('index')
def get_object(self):
return Doctor.objects.first()
产生:
如果将 ModelSelect2Multiple 替换为 ModelSelect2
然后你会得到: