Add hashtag search functionality and create hashtag page template
This commit is contained in:
parent
d75ea17b32
commit
a98d36da14
@ -19,7 +19,7 @@
|
||||
<span>Hashtags:</span>
|
||||
<ul>
|
||||
{% for tag in tags %}
|
||||
<li>#{{ tag }}</li>
|
||||
<li><a href="{% url 'hashtag_search' tag.slug %}">#{{ tag }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
19
innovedus_cms/home/templates/home/hashtag_page.html
Normal file
19
innovedus_cms/home/templates/home/hashtag_page.html
Normal file
@ -0,0 +1,19 @@
|
||||
{% extends "base.html" %}
|
||||
{% load wagtailcore_tags %}
|
||||
{% block content %}
|
||||
<nav class="breadcrumbs" aria-label="breadcrumb">
|
||||
<ol>
|
||||
<li>
|
||||
{% if site_root %}
|
||||
<a href="{{ site_root.url }}">首頁</a>
|
||||
{% else %}
|
||||
<a href="/">首頁</a>
|
||||
{% endif %}
|
||||
</li>
|
||||
<li><span>標籤</span></li>
|
||||
<li><span>#{{ tag.name }}</span></li>
|
||||
</ol>
|
||||
</nav>
|
||||
|
||||
{% include "home/includes/page-article-list.html" %}
|
||||
{% endblock %}
|
||||
43
innovedus_cms/home/views.py
Normal file
43
innovedus_cms/home/views.py
Normal file
@ -0,0 +1,43 @@
|
||||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
|
||||
from taggit.models import Tag
|
||||
from wagtail.models import Site
|
||||
|
||||
from .models import ArticlePage, PAGE_SIZE
|
||||
|
||||
|
||||
def hashtag_search(request, slug):
|
||||
tag = get_object_or_404(Tag, slug=slug)
|
||||
articles = (
|
||||
ArticlePage.objects.live()
|
||||
.filter(tags__slug=slug)
|
||||
.order_by("-date")
|
||||
)
|
||||
|
||||
paginator = Paginator(articles, PAGE_SIZE)
|
||||
page_number = request.GET.get("page")
|
||||
|
||||
try:
|
||||
page_obj = paginator.page(page_number)
|
||||
except PageNotAnInteger:
|
||||
page_obj = paginator.page(1)
|
||||
except EmptyPage:
|
||||
page_obj = paginator.page(paginator.num_pages)
|
||||
|
||||
site = Site.find_for_request(request)
|
||||
site_root = site.root_page if site else None
|
||||
|
||||
context = {
|
||||
"tag": tag,
|
||||
"category_sections": [
|
||||
{
|
||||
"title": f"#{tag.name}",
|
||||
"items": page_obj,
|
||||
"url": request.path,
|
||||
}
|
||||
],
|
||||
"site_root": site_root,
|
||||
}
|
||||
|
||||
return render(request, "home/hashtag_page.html", context)
|
||||
@ -7,11 +7,14 @@ from wagtail import urls as wagtail_urls
|
||||
from wagtail.documents import urls as wagtaildocs_urls
|
||||
|
||||
from search import views as search_views
|
||||
from home import views as home_views
|
||||
|
||||
urlpatterns = [
|
||||
path("django-admin/", admin.site.urls),
|
||||
path("admin/", include(wagtailadmin_urls)),
|
||||
path("documents/", include(wagtaildocs_urls)),
|
||||
# use <str:slug> so Unicode tag slugs (e.g. 台北美食) still resolve
|
||||
path("tags/<str:slug>/", home_views.hashtag_search, name="hashtag_search"),
|
||||
path("search/", search_views.search, name="search"),
|
||||
]
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user