Django:django-autocomplete-light 无法正常工作

Posted

技术标签:

【中文标题】Django:django-autocomplete-light 无法正常工作【英文标题】:Django: django-autocomplete-light does not work properly 【发布时间】:2016-09-26 19:55:58 【问题描述】:

我在 django 1.8 中使用django-autocomplete-light。但我不知道如何在forms.py 中使用它。我看到的是一个选择字段,而不是自动完成字段。我按照here 的说明进行操作。

models.py 我有:

class icd_10(models.Model):
    id = models.IntegerField(unique=True,primary_key=True,null=False,blank=False)
    icd_10_desc = models.CharField('ICD-10 description',max_length=80,null=True,blank=True)
    icd_10_code = models.CharField('ICD-10 code',max_length=10,null=True,blank=True)


    def __str__(self):
        return str(self.icd_10_desc)

class Diagnosis(models.Model):

    diagnosis_option = models.ManyToManyField(DiagnosisOption)
    record_of_genotype = models.CharField(max_length=45,null=True,blank=True)
    icd_10_desc = models.ManyToManyField(icd_10)
    patient = models.ForeignKey(Demographic)

    def __str__(self):
        return str(self.patient)

settings.py 我有:

INSTALLED_APPS = (
    'dal',
    'dal_select2',
    'django.contrib.admin',
     ...
)

views.py 我有:

class IcdTenAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        #Don't forget to filter out results depending on the visitor !
        if not self.request.user.is_authenticated():
            return icd_10.objects.none()

        qs = icd_10.objects.all()

        if self.q:
             qs = qs.filter(icd_10_desc__istartswith=self.q)

        return qs

urls.py 我有:

url(r'^icd10-autocomplete/$','eReg.views.IcdTenAutocomplete',name='icd10-autocomplete'),

forms.py 我有:

class DiagnosisForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):

        super(DiagnosisForm, self).__init__(*args, **kwargs)
self.helper.layout = Layout(
            Fieldset (
                # 'patient',
                '<b>Diagnosis information</b>',

                'diagnosis_option',
                'icd_10_desc',

                ),


            FormActions(
                Submit('submit', "Save changes"),
                Submit('cancel',"Cancel")
            ),
        )
        self.helper.form_tag = False
        self.helper.form_show_labels = True

    class Meta:
        model = Diagnosis
        exclude = ['patient', 'author']

        list_display = ('patient', 'pub_date', 'author')
        widgets='icd10_desc' : autocomplete.ModelSelect2Multiple(url='icd10-autocomplete')

当我直接从浏览器运行它时,它可以正常工作。我明白了

"pagination": "more": false, "results": ["text": "Thalassemia", "id": 8, "text": "Thalassemia trait", "id": 12, "text": "Thalassemia, unspecified", "id": 15]

那么,我错过了正确的 javascript 吗?

【问题讨论】:

【参考方案1】:

我找到了解决方案!我必须在我的模板中包含以下代码:

% block footer %

<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>

 frm.media 

% endblock %

以及我的模板从中继承数据的 base.html 中的以下代码。

<link src="/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link src="/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="/static/collected/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/collected/autocomplete_light/select2.js"></script> 

【讨论】:

以上是关于Django:django-autocomplete-light 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

有人使用过 Django 和 JQuery 自动完成功能吗?

Django - 学习目录

Django初识

Django之路

Django系列

django 错误