将基本搜索添加到Django站点

时间:2018-06-07 06:18:15

标签: django

我正在尝试在详细视图中实现基本搜索。以下是视图的代码。详细视图具有get_queryset()函数,它实际上链接到detail.html 一旦我点击搜索错误:/ error / 1 /中的FieldError 无法解析关键字' insurance_name'进入田野。选项包括:client_name,clientinfo,id

urls.py

app_name = 'update'

urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='detail'),
url(r'client/add/$',views.Clientcreate.as_view(), name='client-create'),
url(r'info/add/$',views.ClientInfocreate.as_view(), name='info-create'),
url(r'client/(?P<pk>[0-9]+)/$',views.ClientUpdate.as_view(), name='client- 
update'),
url(r'client/(?P<pk>[0-9]+)/delete/$',views.ClientDelete.as_view(), 
name='client-delete'),
url(r'info/(?P<pk>[0-9]+)/$',views.ClientInfoUpdate.as_view(), name='info- 
edit'),
]

views.py

class IndexView(generic.ListView):
template_name = 'update/index.html'
context_object_name = 'all_client'
def get_queryset(self):
    return Clients.objects.all()


class DetailView(generic.DetailView):
model = Clients
context_object_name = 'client'
template_name = 'update/detail.html'

paginate_by = 10

def get_queryset(self):
    result = super(DetailView, self).get_queryset()

    query = self.request.GET.get('q')
    if query:
        # query_list = query.split()
        result = result.filter(Q(insurance_name__icontains=query) | 
      Q(update__icontains=query))

    return result

的index.html

{% for client in all_client %}
            <div class="col-xs-18 col-sm-6 col-md-3" align="center">
                <div class="thumbnail">

                    <div class="caption">
                        <!--<img src="{% static 'update/img/top.png' %}" 
  class="img-rounded" height="100" width="100">-->
                        <h2>{{ client.client_name }}</h2>



                        <!-- View Details -->
                        <a href="{% url 'update:detail' client.id %}" 
  class="btn btn-primary btn-sm" role="button">View Details</a>

                        <!-- Delete -->
                        <form action="{% url 'update:client-delete' 
 client.id%}" method="post" style="display: inline;">
                            {% csrf_token %}
                            <input type="hidden" name="client_id" value="" />
                            <button type="submit" class="btn btn-default btn-sm">
                                <span class="glyphicon glyphicon-trash"></span>
                            </button>

                        </form>
                         <form action="{% url 'update:client-update' client.id %}" method="post" style="display: inline;">
                            {% csrf_token %}
                            <input type="hidden" name="client_id" value="" />
                            <button type="submit" class="btn btn-default btn-sm">
                                <span class="glyphicon glyphicon-edit"></span>
                            </button>
                        </form>

detail.html

<form class="navbar-form navbar-left" role="search" method="get" action="{% 
 url 'update:detail' client.id %}">
            <div class="form-group">
                <input type="text" class="form-control" name="q" value="{{ 
request.GET.q }}">
            </div>
            <button type="submit" class="btn btn-default">Search</button>
        </form>
<div class="container">

<h3>{{client.client_name}}<span class="badge">2</span></h3>
<a href="{% url 'update:info-create'%}">Add Client Info</a>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
 <tr>
    <th scope="col"><small>INCURANCE NAME</small></th>
    <th scope="col"><small>CPT-CODE</small></th>
    <th scope="col"><small>SUBJECT</small></th>
    <th scope="col"><small>UPDATE</small></th>
    <th scope="col"><small> DATE</small> </th>
    <th scope="col"><small> UPDATE DATE</small> </th>
    <th scope="col"><small> UPDATED BY</small> </th>
    <th scope="col"><small> STATUS</small> </th>
 </tr>
 </thead>
 <tbody>
 <tr>
   {% for item in client.clientinfo_set.all %}
    <td><small> {{ item.insurance_name }}</small></td>
    <td><small>{{ item.cpt_code }}</small></td>
    <td><small> {{ item.subject }}</small></td>
    <td><small><p>{{ item.update }}</p></small></td>
    <td><small><p>{{ item.date }}</p></small></td>
    <td><small>{{ item.update_date }}</small></td>
    <td><small>{{ item.update_by }}</small></td>
    <td><small><a href="{% url 'update:info-edit' client.id%}">Edit Info</a> 
   </small></td>
  </tr>

models.py

class Clients(models.Model):
client_name = models.CharField(max_length=250)

def get_absolute_url(self):
    return reverse('update:detail',kwargs={'pk' : self.pk})

def __str__(self):
    return self.client_name


 class ClientInfo(models.Model):
client = models.ForeignKey(Clients, on_delete=models.CASCADE)
insurance_name = models.CharField(max_length=100)
cpt_code = models.CharField(max_length=100)
subject = models.CharField(max_length=250)
update = models.CharField(max_length=10000)
date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
update_by = models.CharField(max_length=250)

def get_absolute_url(self):
    return reverse('update:index')

def __str__(self):
    return self.insurance_name

once i enter in the search box and click enter the results should be populated but by default the entire table should be visible

1 个答案:

答案 0 :(得分:0)

好像你需要这样做。

def get_context_data(self, **kwargs):
    context = super(DetailView, self).get_context_data(**kwargs)
    #context = super().get_context_data(**kwargs) for python3
    clientinfo = context['client'].clientinfo_set.all()
    query = self.request.GET.get('q')
    if query:
        clientinfo = clientinfo.filter(Q(insurance_name__icontains=query) | Q(update__icontains=query))
    context['clientinfo'] = clientinfo
    return context

然后在模板中

{% for item in clientinfo %}