我进入Python-Django 2个月,我没有完整的体验来继续我想做的事情。基于django-admin创建的类别在引导下拉列表中创建Django过滤器
我喜欢营造一种过滤器或过滤器下拉这样,任何人都可以从for循环渲染类别的下拉过滤或按类别搜索选择。我成功地创建一个完整的搜索与草垛与嗖(您可以测试我的草垛搜索here与类称为营销搜索)
有人可以阐明我怎么可以根据类别来筛选列表更多的光线在循环落下?我真的很困惑。
下面的代码到我的工作草堆搜索
search_indexes.py
import datetime
from haystack import indexes
from haystack.query import EmptySearchQuerySet
from .models import Mentor
from .models import *
class MentorIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
author1 = indexes.EdgeNgramField(model_attr='first_name')
author2 = indexes.EdgeNgramField(model_attr='last_name')
author3 = indexes.EdgeNgramField(model_attr='category')
author4 = indexes.EdgeNgramField(model_attr='email')
author5 = indexes.EdgeNgramField(model_attr='location')
def get_model(self):
return Mentor
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
home.html的
<form method="get" action="/search/" class="navbar-form">
<div class="form-group" style="display:inline;">
<div class="input-group" style="display:table;">
<input class="form-control" name="q" placeholder="Search Mentors Here. You can search by Location, Email, Name, e.t.c." autocomplete="off" autofocus="autofocus" type="text">
<span class="input-group-btn" style="width:1%;">
<button class="btn btn-danger" type="submit">
<span class=" glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
</form>
程式网址配置(url.py)
from django.conf.urls import include, url
from mentoring_application.views import profile
urlpatterns = [
url(r'^profile/(?P<pk>\d+)/$', profile, {}, name='mentor-profile'),
]
项目URL配置(url.py)
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from mentoring_application.views import HomeView, profile
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', HomeView.as_view()),
url(r'^mentor/', include('mentoring_application.urls', namespace='mentor')),
url(r'^search/', include('haystack.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.views import View
from .models import *
from .models import Mentor
from django.shortcuts import render, get_object_or_404
# Create your views here.
class HomeView(View):
# @staticmethod
def get(self, request, *args, **kwargs):
mentors = Mentor.objects.all()
return render(request, "mentoring_application/home.html", {"mentors": mentors})
def profile(request, pk, template='mentoring_application/profile.html'):
mentor = get_object_or_404(Mentor, pk=pk) # pk is primary key, so url will be site.com/profile/3
context = {'mentor': mentor}
return render(request, template, context)
为了更清楚,此图片显示了一个类别过滤器我喜欢做。
什么是发送模板数据的视图方法?你可以发布吗? –
@TimS。我不确定我是否理解你的问题,但我已根据要求进行了更新。但是,我希望按类别创建过滤器。 –
通常,它的工作原理是您的视图基于您的过滤条件构建对象,然后将过滤后的对象/查询集发送到模板以作为下拉列表呈现。我之前没有专门做过,但是它的视角将数据发送到模板进行渲染。 –