django-autocomplete-light在窗体中显示空的下拉列表 [英] django-autocomplete-light displays empty dropdown in the form

查看:2627
本文介绍了django-autocomplete-light在窗体中显示空的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用django-autocomplete-light
从本教程中,

解决方案

我遇到同样的问题。这是一个很好的参考: django-autocompletion-轻松简单的外键完成显示不可编辑的下拉窗口小部件



确保您正在加载头文件中的jQuery,并使用 {{form.media}} 标签。



这是我在头文件中加载jquery的方法: / p>

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery .min.js>< /脚本> 
< script src =http://code.jquery.com/ui/1.9.2/jquery-ui.js>< / script>

此外,你是否试过去 tenant-autocomplete / url,看看你是否获得了该页面上的结果,就像在教程 https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html



如果可以'从url得到的结果应该是您修复的第一个错误。


I am trying to use django-autocomplete-light From this tutorial https://github.com/yourlabs/django-autocomplete-light/blob/master/docs/tutorial.rst

I Installed it with pip and added it to my settings file

INSTALLED_APPS = (
    'dal',
    'dal_select2',

for my tenant value

My tenant model is

class Tenant(CommonInfo):
    version = IntegerVersionField( )

    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    def __unicode__(self):
        return u'%s %s %s ' % ("#", self.id,"first_name", self.first_name, "last_name")

In my autocomplete view:

from django.shortcuts import render
from dal import autocomplete
from client.models import Tenant


    class TenantAutocomplete(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 Tenant.objects.none()

            qs = Tenant.objects.all()

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

            return qs

in autocomplete url

from django.conf.urls import url, include,patterns
from client import views 

    urlpatterns = [
        url(
            r'^tenant-autocomplete/$',
            views.TenantAutocomplete.as_view(),
            name='tenant-autocomplete',
        ), 
    ]

and in form

class LeaseTenantForm(forms.ModelForm):
    tenant = forms.ModelChoiceField(
        queryset=LeaseTenant.objects.all(),
        widget=autocomplete.ModelSelect2(url='tenant-autocomplete')
        )
    class Meta:
        model = LeaseTenant
        exclude = ['lease']

However after rendering in browser I don't see the input text field but the empty dropdown. in actual view

def tenant_new(request,pk,uri):
    lease = get_object_or_404(Lease, pk=pk)
    title = 'tenant'
    uri = _get_redirect_url(request, uri)
    if request.method == "POST":
        form = LeaseTenantForm(request.POST)
        if form.is_valid():
            tenant = form.save(commit=False)  
            tenant.lease = lease      
            tenant.save()
            messages.add_message(request, messages.SUCCESS, str(tenant.id) + "-SUCCESS Object created sucssefully")


            return redirect(uri)
    else:
        form = LeaseTenantForm()
    return render(request, 'object_edit.html', {'form': form, 'title': title, 'extend': EXTEND})

My template file

{% block title %}
        Add/Edit {{ title  }}
{% endblock title %}


{% block content %}

<div class="container">
        <div class="row">
            <div class="col-md-6 col-sm-12">
                    <h4>{{ title  }}</h4>
                    <br><div class="center">
                <form method="POST"  class="form" action="" method="get">
                    <div class="form-group">
                        {% csrf_token %}
                        {{ form|crispy}}



                        {{ form.media }}

                        <BR><BR>
                        <button type="submit" class="btn btn-primary btn-primary">Save</button>
                    </div>
                </form>
               </div>
            </div>
        </div>
</div>

 {% endblock content %} 

So basically the autocomplete is not working . What should I validate to make it work?

解决方案

I was running into the same problem. This was a good reference: django-autocompletion-light simple foreign key completion shows not editable drop-down widget

Make sure you are loading the jquery in your header file and that you use the {{ form.media }} tag for your form.

Here is how I load the jquery in my header file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

Also, have you tried going to the tenant-autocomplete/ url to see if you get the results on that page like it is mentioned in the tutorial https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html

If you can't get results from the url that should be the first bug you fix.

这篇关于django-autocomplete-light在窗体中显示空的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆