为什么我的搜索功能不起作用?

时间:2016-01-12 14:19:45

标签: python django

我目前正在尝试为我的Django应用实现搜索功能。一切都很好,直到处理实际的搜索查询。说,我搜索“你好”。 Django会返回以下错误:

django.core.exceptions.FieldError
FieldError: Cannot resolve keyword 'title' into field. Choices are: adresse, arbejde, beskrivelse, email, foerste_session, fulde_navn, id, profilbillede, relateret_til_andre_klienter, tidligere_klient, vurder_sidste_session

我的表单(我正在使用ModelForm)和我的搜索功能之间似乎存在某种冲突。我该如何解决我的问题?

这是我的forms.py:

from django import forms

from .models import Client

class ClientForm(forms.ModelForm):


    class Meta:
        model = Client
        fields = (
            'fulde_navn',
            'adresse',
            'email',
            'tidligere_klient',
            'foerste_session',
            'beskrivelse',
            'arbejde',
            'relateret_til_andre_klienter',
            'vurder_sidste_session',
            'profilbillede'
        )

这是我的models.py

from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
import uuid
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse

import os
import uuid

YESNO_CHOICES = (
    (0, 'Ja'),
    (1, 'Nej')
    )


SESSION_CHOICES = (
    (0, '1'),
    (1, '2'),
    (2, '3'),
    (3, '4'),
    (4, '5'),
    )

def upload_to_location(instance, filename):
    blocks = filename.split('.')
    ext = blocks[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    instance.title = blocks[0]
    return os.path.join('uploads/', filename)
# Create your models here.

class Client(models.Model):
    fulde_navn = models.CharField('Navn', max_length=75)
    adresse = models.CharField(max_length=100)
    email = models.EmailField(null=True, blank=True)
    tidligere_klient = models.IntegerField(choices=YESNO_CHOICES, null=True, blank=True)
    foerste_session = models.DateField('Dato for 1. session', null=True, blank=True)
    beskrivelse = models.TextField('Noter', null=True, blank=True)
    arbejde = models.CharField('Arbejde', max_length=200)
    relateret_til_andre_klienter = models.IntegerField(choices=YESNO_CHOICES, null=True, blank=True)
    vurder_sidste_session = models.IntegerField(choices=SESSION_CHOICES, null=True, blank=True)
    profilbillede = models.ImageField('Profilbillede',
                                upload_to='profile_pics/%Y-%m-%d/',
                                null=True,
                                blank=True)


    def __unicode__(self):
        return self.fulde_navn

    def get_absolute_url(self):
        return reverse(viewname="client_detail", args=[self.id])

最后我的views.py我的搜索功能位于:

from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from . import models
from .forms import ClientForm
# Create your views here.

class ClientsListView(ListView):
    model = models.Client
    template_name = 'clients/list.html'
    paginate_by = 20


class SearchListView(ClientsListView):

    def get_queryset(self):
        incoming_query_string = self.request.GET.get('query', '')
        return models.Client.objects.filter(title__icontains=incoming_query_string)


class ClientsDetailView(DetailView):
    model = models.Client
    template_name = 'clients/detail.html'


def client_create(request):
    if request.method == 'POST':
        form = ClientForm(request.POST)
        if form.is_valid():
            client = form.save()
            client.save()
            return redirect('client_detail', pk=client.pk)
    else:
        form = ClientForm()

    return render(request, 'clients/form.html', {'form': form})

def client_edit(request, pk):
    client = get_object_or_404(models.Client, pk=pk)
    if request.method == "POST":
        form = ClientForm(request.POST, instance=client)
        if form.is_valid():
            client = form.save()
            client.save()
            return redirect('client_detail', pk=client.pk)
    else:
        form = ClientForm(instance=client)
    return render(request, 'clients/form.html', {'form': form})


class ClientsUpdateView(UpdateView):
    model = models.Client
    template_name = 'clients/form.html'

我该如何解决这个问题?我想这样做,以便我的用户能够在我的列表中搜索客户端。

1 个答案:

答案 0 :(得分:1)

在这一行:

return models.Client.objects.filter(title__icontains=incoming_query_string)

您正在尝试检查字段title是否包含incoming_query_string。但是,您的ClientForm没有此类字段。

您应该使用其中列出的某个字段,或将title添加为字段。