我正在尝试向项目添加自动完成光,但是我不能。如果我想找到任何形式的内容,则表示:无法加载结果,TypeError:'bool'对象不可调用。我正在使用python版本3.7和django-autocomplete-light版本3.3.5
该项目是网络应用程序,用于跟踪分配给用户的订单。
我添加到mysite / settings.py:
INSTALLED_APPS = [
'dal',
'dal_select2',
'crispy_forms',
'myapp',
添加到mysite / myapp / urls.py:
path('autocomplete/', login_required(views.OrderAutocomplete.as_view()), name="autocomplete"),
到base.html:
<head>
{% load staticfiles %}
...
<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>
并转到new_order。HTML:
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
{{ form.media }}
<input type="submit">
</form>
我在mysite / myapp / views.py中添加了:
class ObjednavkaAutocomplete(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 Objednavka.objects.none()
qs = Order.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
并添加到mysite / myapp / forms.py中:
class OrderForm(forms.ModelForm):
user_auto = forms.ModelChoiceField(queryset=Order.objects.all(), widget = autocomplete.ModelSelect2(url='autocomplete'))
class Meta:
model = Order
fields = ["user_auto"]
mysite / myapp / models.py:
from dal import autocomplete
class Order(models.Model):
...
user = models.ForeignKey(MyUser, on_delete=models.CASCADE)
class MyUser(models.Model):
eid = models.CharField(max_length=7)
对不起,我发布了这么多代码。
答案 0 :(得分:0)
替换
if not self.request.user.is_authenticated()
使用
if not self.request.user.is_authenticated
在Django> 2.0中,它是属性。参见docs。