【发布时间】:2018-07-24 05:22:29
【问题描述】:
我正在尝试在我的页面中添加搜索选项。我已经尝试了此链接“https://docs.djangoproject.com/en/1.11/intro/tutorial01/”中标准 django 文档中给出的方法,但我的搜索功能不起作用。只有 url 从 http://localhost:8000/contact this 更改为 http://localhost:8000/contact/your-contact/?q=harini 但不会过滤和显示数据。我正在使用 django 1.11。有人可以帮我吗?在此先感谢
models.py
from __future__ import unicode_literals
from django.db import models
class ContactModel(models.Model):
name = models.CharField(max_length=200)
dob = models.DateField()
phone_number = models.IntegerField()
gender = models.BooleanField()
address = models.CharField(max_length=200)
views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.views import generic
from django.template.response import TemplateResponse
from django.views.generic import CreateView
from django.urls import reverse_lazy
from .models import ContactModel
from .forms import ContactForm
class ContactView(CreateView):
model_class = ContactModel
form_class = ContactForm
success_url = reverse_lazy('contact-thanks')
initial = {'name': 'value'}
template_name = 'contact/contact.html'
def get(self, request, *args, **kwargs):
model = self.model_class()
return render(request, self.template_name, {'model': ContactForm})
def post(self, request, *args, **kwargs):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('thanks/')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
class ContactlistView(CreateView):
model = ContactModel
template_name = 'contact/contactlist.html'
def search(request):
query = request.GET['q']
t = loader.get_template('template/contact/contactlist.html')
c = Context({ 'query': query,})
if query:
results = Contact.objects.filter(Q(name__icontains=query) |
Q(dob__icontains=query) |
Q(address__icontains=query) |
Q(gender__icontains=query) |
Q(phone_number__icontains=query))
return render(request, 'contactlist.html', {'form': form})
else:
return render(request, 'contactlist.html', {'form': form})
urls.py
from django.conf.urls import url
from . import views
app_name = 'contact'
urlpatterns = [
url(r'^$', views.ContactView.as_view(), name='contact'),
url(r'^your-contact/$', views.ContactView.as_view(), name='contact'),
url(r'^your-contact/thanks/$', views.ContactView.as_view(), name='contact'),
url(r'^contact-list/$', views.ContactlistView.as_view(), name='contactlist'),
url(r'^contact-list/your-contact/$', views.ContactlistView.as_view(), name='contactlist'),
]
templates/contact/contactlist.html
<form method="get" action="contact-list/">
<input type="text" id="searchBox" class="input-medium search-query" name="q" placeholder="Search" value= "{{ request.GET.q }}">
{% if contact %}
{% for each_contact in contacts %}
<h3>Name: {{ contact.name }}</h3>
<p>Dob: {{ contact.dob }}</p>
<p>Gender: {{ contact.gender }}</p>
<p>Address: {{ contact.address }}</p>
<p>Phone Number: {{ contact.phone_number}}</p>
{% endfor %}
{% endif %}
<input type="submit" class="btn" value="Search" >
</form>
【问题讨论】:
-
将
if q:替换为if query:。 -
它不起作用@Selcuk
-
好的,您在模板中的什么位置显示搜索结果?
-
我认为您应该将 def search(request) 替换为 def get (self, request) 并将您的联系人列表网址指向 ContactListView