mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-26 09:34:50 +00:00
Added search app
This commit is contained in:
parent
effa01919f
commit
b7b5aec0c7
5 changed files with 91 additions and 0 deletions
0
wagtail/project_template/project_name/search/__init__.py
Normal file
0
wagtail/project_template/project_name/search/__init__.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
{% templatetag openblock %} extends "base.html" {% templatetag closeblock %}
|
||||
{% templatetag openblock %} load static wagtailcore_tags {% templatetag closeblock %}
|
||||
|
||||
{% templatetag openblock %} block body_class {% templatetag closeblock %}template-searchresults{% templatetag openblock %} endblock {% templatetag closeblock %}
|
||||
|
||||
{% templatetag openblock %} block title {% templatetag closeblock %}Search{% templatetag openblock %} endblock {% templatetag closeblock %}
|
||||
|
||||
{% templatetag openblock %} block content {% templatetag closeblock %}
|
||||
<h1>Search</h1>
|
||||
|
||||
<form action="{% templatetag openblock %} url 'search' {% templatetag closeblock %}" method="get">
|
||||
<input type="text" name="query"{% templatetag openblock %} if search_query {% templatetag closeblock %} value="{% templatetag openvariable %} search_query {% templatetag closevariable %}{% templatetag openblock %} endif {% templatetag closeblock %}">
|
||||
<input type="submit" value="Search">
|
||||
</form>
|
||||
|
||||
{% templatetag openblock %} if search_picks {% templatetag closeblock %}
|
||||
<ul>
|
||||
{% templatetag openblock %} for pick in search_picks {% templatetag closeblock %}
|
||||
<li>
|
||||
<h2><a href="{% templatetag openvariable %} pick.page.url {% templatetag closevariable %}">{% templatetag openvariable %} pick.page {% templatetag closevariable %}</a></h2>
|
||||
<p>{% templatetag openvariable %} pick.description {% templatetag closevariable %}</p>
|
||||
</li>
|
||||
{% templatetag openblock %} endfor {% templatetag closeblock %}
|
||||
</ul>
|
||||
{% templatetag openblock %} endif {% templatetag closeblock %}
|
||||
|
||||
{% templatetag openblock %} if search_results {% templatetag closeblock %}
|
||||
<ul>
|
||||
{% templatetag openblock %} for result in search_results {% templatetag closeblock %}
|
||||
<li>
|
||||
<h4><a href="{% templatetag openblock %} pageurl result {% templatetag closeblock %}">{% templatetag openvariable %} result {% templatetag closevariable %}</a></h4>
|
||||
{% templatetag openblock %} if result.search_description {% templatetag closeblock %}
|
||||
{% templatetag openvariable %} result.search_description|safe {% templatetag closevariable %}
|
||||
{% templatetag openblock %} endif {% templatetag closeblock %}
|
||||
</li>
|
||||
{% templatetag openblock %} endfor {% templatetag closeblock %}
|
||||
</ul>
|
||||
|
||||
{% templatetag openblock %} if search_results.has_previous {% templatetag closeblock %}
|
||||
<a href="{% templatetag openblock %} url 'search' {% templatetag closeblock %}?query={% templatetag openvariable %} search_query|urlencode {% templatetag closevariable %}&page={% templatetag openvariable %} search_results.previous_page_number {% templatetag closevariable %}">Previous</a>
|
||||
{% templatetag openblock %} endif {% templatetag closeblock %}
|
||||
|
||||
{% templatetag openblock %} if search_results.has_next {% templatetag closeblock %}
|
||||
<a href="{% templatetag openblock %} url 'search' {% templatetag closeblock %}?query={% templatetag openvariable %} search_query|urlencode {% templatetag closevariable %}&page={% templatetag openvariable %} search_results.next_page_number {% templatetag closevariable %}">Next</a>
|
||||
{% templatetag openblock %} endif {% templatetag closeblock %}
|
||||
{% templatetag openblock %} elif search_query {% templatetag closeblock %}
|
||||
No results found
|
||||
{% templatetag openblock %} endif {% templatetag closeblock %}
|
||||
{% templatetag openblock %} endblock {% templatetag closeblock %}
|
||||
39
wagtail/project_template/project_name/search/views.py
Normal file
39
wagtail/project_template/project_name/search/views.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
from django.shortcuts import render
|
||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||
|
||||
from wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailsearch.models import Query, EditorsPick
|
||||
|
||||
|
||||
def search(request):
|
||||
search_query = request.GET.get('query', None)
|
||||
page = request.GET.get('page', 1)
|
||||
|
||||
# Search
|
||||
if search_query:
|
||||
search_results = Page.objects.live().search(search_query)
|
||||
query = Query.get(search_query)
|
||||
|
||||
# Record hit
|
||||
query.add_hit()
|
||||
|
||||
# Get search picks
|
||||
search_picks = query.editors_picks.all()
|
||||
else:
|
||||
search_results = Page.objects.none()
|
||||
search_picks = EditorsPick.objects.none()
|
||||
|
||||
# Pagination
|
||||
paginator = Paginator(search_results, 10)
|
||||
try:
|
||||
search_results = paginator.page(page)
|
||||
except PageNotAnInteger:
|
||||
search_results = paginator.page(1)
|
||||
except EmptyPage:
|
||||
search_results = paginator.page(paginator.num_pages)
|
||||
|
||||
return render(request, 'search/search.html', {
|
||||
'search_query': search_query,
|
||||
'search_results': search_results,
|
||||
'search_picks': search_picks,
|
||||
})
|
||||
|
|
@ -47,6 +47,7 @@ INSTALLED_APPS = (
|
|||
'wagtail.wagtailredirects',
|
||||
'wagtail.wagtailforms',
|
||||
|
||||
'{{ project_name }}.search',
|
||||
'{{ project_name }}.home',
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ urlpatterns = [
|
|||
url(r'^admin/', include(wagtailadmin_urls)),
|
||||
url(r'^documents/', include(wagtaildocs_urls)),
|
||||
|
||||
url(r'^search/$', '{{ project_name }}.search.views.search', name='search'),
|
||||
|
||||
url(r'', include(wagtail_urls)),
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue