2016-05-13 81 views
2

我讀過http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#using-autocompletes-outside-the-admin使用Django的自動完成光外管理視圖

,並試圖用一個簡單的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> 

我只想用自己的不錯選擇2小部件列出與醫生IDS醫生​​的名字,如自動完成值。我可以在沒有django-autocomplete-light的情況下做到這一點,但我明確地想要學習如何使用看起來更好的widget。 有人能指導我讓它工作嗎?

回答

0

我得到它:

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() 

其產生:

enter image description here

如果用ModelSelect2替換ModelSelect2Multiple 那麼你會得到: enter image description here

+0

我很高興你解決了它。我會在我的答覆的相關部分糾正疏忽,謝謝! – raratiru

1

更新

應覆蓋'doctor'屬性的窗口小部件的int Patient模型,而不是Doctor型號:

class DocForm(autocomplete.FutureModelForm): 
    class Meta: 
     model = Patient 
     fields = ('doctor',) # or ('__all__') 
     widgets = { 
      'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'), 
     } 

同時,確保參觀reverse('doc-autocomplete')網址給出了預期的結果,如登錄用戶所述,如autocomplete tutorial.


試圖解決類似的問題,我發現我沒有在我的模板中包含{{ form.media }}

它在docs描述:

{% block footer %} 
<script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script> 

{{ form.media }} 
{% endblock %} 
+0

謝謝。我嘗試過,但它仍然不加載小部件;沒有javascript錯誤或python錯誤。 – max

+0

@max在自動完成燈庫中,有一個[工作示例](https://github.com/yourlabs/django-autocompletelight/tree/master/test_project/select2_outside_admin)該字段。有沒有可能在那裏找到解決方案? – raratiru

+0

我已經在我的文章中看到了該鏈接。必須有一些我錯過的小事。 – max