update. 优化搜索功能
This commit is contained in:
@@ -7,6 +7,8 @@ app_name = 'front' # 可选,用于命名空间(如 {% url 'main:index' %}
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('<str:main>.html', views.section, name='section'),
|
||||
path('list/search.html', views.search_list, name='search_list'),
|
||||
path('list/search/<str:sec>.html', views.search_section_list, name='search_section_list'),
|
||||
path('list/news.html', views.main_news_list, name='news_main'),
|
||||
path('list/<str:main>.html', views.main_article_list, name='main_list'),
|
||||
path('list/<str:main>/<str:sub>.html', views.sub_article_list, name='sub_list'),
|
||||
|
||||
+56
-2
@@ -206,7 +206,7 @@ def sub_news_list(request, sub: str):
|
||||
logger.info(f"主板块 {main_section} 子板块 {sub_section}")
|
||||
if not sub_section or not main_section:
|
||||
raise Http404("不存在的板块")
|
||||
news_list = sub_section.news_set.filter(is_published=True).order_by('-created_at')[:21]
|
||||
news_list = sub_section.news_set.filter(is_published=True).order_by('-created_at')
|
||||
news_list = custom_pagination(news_list, request)
|
||||
|
||||
context = {
|
||||
@@ -219,6 +219,54 @@ def sub_news_list(request, sub: str):
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
|
||||
def search_list(request):
|
||||
sections = MainMenu.objects.filter(parent__visible=True, visible=True, parent__isnull=False, ).all()
|
||||
articles_qs = Article.objects.filter(is_published=True).all()
|
||||
q = request.GET.get('q')
|
||||
if q:
|
||||
articles_qs = articles_qs.filter(
|
||||
Q(title__icontains=q) | Q(content__icontains=q)
|
||||
)
|
||||
articles = custom_pagination(articles_qs, request)
|
||||
main_section = {
|
||||
'title': '搜索中心',
|
||||
'code': 'search'
|
||||
}
|
||||
context = {
|
||||
'main_section': main_section,
|
||||
'sections': sections,
|
||||
'articles': articles,
|
||||
"q": q,
|
||||
}
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
|
||||
def search_section_list(request, sec):
|
||||
sections = MainMenu.objects.filter(parent__visible=True, visible=True, parent__isnull=False, ).all()
|
||||
articles_qs = Article.objects.filter(is_published=True).all()
|
||||
q = request.GET.get('q')
|
||||
sec = sections.filter(code=sec).first()
|
||||
if q:
|
||||
articles_qs = articles_qs.filter(
|
||||
Q(title__icontains=q) | Q(content__icontains=q)
|
||||
)
|
||||
if sec:
|
||||
articles_qs = articles_qs.filter(section=sec)
|
||||
articles = custom_pagination(articles_qs, request)
|
||||
main_section = {
|
||||
'title': '搜索中心',
|
||||
'code': 'search'
|
||||
}
|
||||
context = {
|
||||
'main_section': main_section,
|
||||
'sections': sections,
|
||||
'articles': articles,
|
||||
"q": q,
|
||||
"current_sec": sec,
|
||||
}
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
|
||||
def main_article_list(request, main):
|
||||
"""信息公开"""
|
||||
logger.info(f"正在访问 {main}")
|
||||
@@ -251,11 +299,17 @@ def sub_article_list(request, main: str, sub: str):
|
||||
if not sub_section or not main_section:
|
||||
raise Http404("不存在的板块")
|
||||
articles = sub_section.article_set.filter(is_published=True).order_by('-created_at')
|
||||
q = request.GET.get('q')
|
||||
if q:
|
||||
articles = articles.filter(
|
||||
Q(title__icontains=q) | Q(content__icontains=q)
|
||||
)
|
||||
articles = custom_pagination(articles, request)
|
||||
context = {
|
||||
'main_section': main_section,
|
||||
'sub_section': sub_section,
|
||||
'sections': sections.filter(parent__code=main),
|
||||
'articles': articles
|
||||
'articles': articles,
|
||||
"q": q,
|
||||
}
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
+3
-3
@@ -214,6 +214,9 @@ STATICFILES_DIRS = (
|
||||
os.path.join(os.path.join(BASE_DIR, 'static')),
|
||||
)
|
||||
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
# 日志配置
|
||||
LOGGING = {
|
||||
'version': 1, # 版本
|
||||
@@ -293,9 +296,6 @@ CKEDITOR_CONFIGS = {
|
||||
'width': '100%',
|
||||
},
|
||||
}
|
||||
STATIC_URL = '/static/'
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
customColorPalette = [
|
||||
{
|
||||
|
||||
@@ -169,13 +169,16 @@ body {
|
||||
}
|
||||
|
||||
.search-btn:hover {
|
||||
background: #1e3a8a; /* 悬停时变为主题色 */
|
||||
background: #c62828; /* 悬停时变为主题色 */
|
||||
color: #fff; /* 图标变白 */
|
||||
width: 30px; /* 悬停时稍微变大 */
|
||||
height: 30px; /* 悬停时稍微变大 */
|
||||
}
|
||||
|
||||
/* 如果使用的是 FontAwesome 等图标库,确保图标大小合适 */
|
||||
.search-btn i {
|
||||
font-size: 18px;
|
||||
.search-btn img {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
|
||||
.user-icons {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1775128526851" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5418" width="16" height="16" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M644.096 251.904a277.333333 277.333333 0 1 0-392.192 392.192 277.333333 277.333333 0 0 0 392.192-392.192zM191.573333 191.573333a362.666667 362.666667 0 0 1 541.269334 480.938667l228.053333 228.053333-60.330667 60.330667-228.053333-228.053333A362.709333 362.709333 0 0 1 191.573333 191.573333z" fill="#23418A" p-id="5419"></path></svg>
|
||||
|
After Width: | Height: | Size: 665 B |
+2
-1
@@ -7,6 +7,7 @@
|
||||
<title>{% block title %}{% endblock %}{{ site_info.title }}</title>
|
||||
<link rel="stylesheet" href="{% static 'css/style.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'admin/simpleui-x/fontawesome-free-6.2.0-web/css/fontawesome.css' %}">
|
||||
</head>
|
||||
<body>
|
||||
<header class="top-header">
|
||||
@@ -16,7 +17,7 @@
|
||||
<div>
|
||||
<h1>{{ site_info.title }}</h1>
|
||||
<p>{{ site_info.e_title }}</p>
|
||||
<p>CHANGBAI MOUNTAIN FIRE AND RESCUE BRIGADE</p>
|
||||
{# <p>CHANGBAI MOUNTAIN FIRE AND RESCUE BRIGADE</p>#}
|
||||
</div>
|
||||
</div>
|
||||
<div></div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}{{ article.section.title }} - 长白山消防救援支队{% endblock %}
|
||||
{% block title %}{{ article.section.title }} - {{ site_info.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-4">
|
||||
@@ -9,8 +9,12 @@
|
||||
<ol class="breadcrumb" style="background-color: transparent !important; padding-bottom: 0;">
|
||||
<li class="breadcrumb-item"><a href="/">网站首页</a></li>
|
||||
<!-- 动态显示所属板块 -->
|
||||
<li class="breadcrumb-item"><a href="#">{{ article.section.parent.title }}</a></li>
|
||||
<li class="breadcrumb-item"><a href="#">{{ article.section.title }}</a></li>
|
||||
<li class="breadcrumb-item"><a
|
||||
href="{% url "front:main_list" main=article.section.parent.code %}">{{ article.section.parent.title }}</a>
|
||||
</li>
|
||||
<li class="breadcrumb-item"><a
|
||||
href="{% url "front:sub_list" main=article.section.parent.code sub=article.section.code %}">{{ article.section.title }}</a>
|
||||
</li>
|
||||
<li class="breadcrumb-item active" aria-current="page">文章详情</li>
|
||||
</ol>
|
||||
</nav>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}{{ article.section.title }} - 长白山消防救援支队{% endblock %}
|
||||
{% block title %}{{ article.section.title }} - {{ site_info.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-4">
|
||||
|
||||
@@ -19,9 +19,9 @@
|
||||
<div class="col-12 date-bar">
|
||||
<span style="font-size: 14px;padding-left: 25px;">{{ time_str }}</span>
|
||||
<div class="user-icons">
|
||||
<form class="search-form">
|
||||
<form class="search-form" action="{% url 'front:search_list' %}">
|
||||
<div class="search-wrapper">
|
||||
<input type="text" class="search-input">
|
||||
<input type="text" class="search-input" name="q">
|
||||
<button type="submit" class="search-btn">
|
||||
<img src="{% static "icons/search.svg" %}" alt="">
|
||||
</button>
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load static %}
|
||||
{% block title %}{{ main_section.title }} - {% endblock %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="container row align-items-center justify-content-between">
|
||||
<nav aria-label="breadcrumb" style="margin-left: -10px;">
|
||||
<ol class="breadcrumb" style="background-color: transparent !important;padding-bottom: 0;">
|
||||
<li class="breadcrumb-item"><a href="/" style="color: #23408A; text-decoration: none;">网站首页</a></li>
|
||||
<li class="breadcrumb-item"><a href="/" style="color: #23408A; text-decoration: none;">网站首页</a>
|
||||
</li>
|
||||
<li class="breadcrumb-item active" aria-current="page">
|
||||
<a href="{% url "front:main_list" main=main_section.code %}"
|
||||
style="color: #23408A; text-decoration: none;"
|
||||
@@ -17,7 +20,15 @@
|
||||
</li>
|
||||
</ol>
|
||||
</nav>
|
||||
|
||||
<form class="search-form" action="{% url 'front:search_list' %}">
|
||||
<div class="search-wrapper" style="border: solid 1px #23408a85">
|
||||
<input type="text" class="search-input" name="q">
|
||||
<button type="submit" class="search-btn">
|
||||
<img src="{% static "icons/search_23418A.svg" %}" alt="" style="border-radius: 15px;">
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="row" style="height: 100%;margin: 15px auto;margin-right: 0 !important;">
|
||||
<div class="col-3">
|
||||
<ul class="list-group">
|
||||
@@ -42,6 +53,27 @@
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% elif '/search' in request.path %}
|
||||
<li class="list-group-item" style="background: #23418A; color: #f5f5f5">
|
||||
<a href="{% url "front:main_list" main=main_section.code %}"
|
||||
style="color: #F5F5F5;text-decoration: none;">
|
||||
<h5 style="margin: 0; padding: 0; line-height: 1.2;">{{ main_section.title }}</h5>
|
||||
</a>
|
||||
</li>
|
||||
{% for sec in sections %}
|
||||
{% if sec.code == current_sec.code %}
|
||||
<li class="list-group-item" style="background: #e9ecef;">
|
||||
<a href="{% url "front:search_section_list" sec=current_sec %}?{% if q %}q={{ q }}{% endif %}"
|
||||
style="color: #23418A; text-decoration: none; font-weight: bold;"> {{ sec.title }}</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="list-group-item">
|
||||
<a href="{% url "front:search_section_list" sec=sec.code %}?{% if q %}q={{ q }}{% endif %}"
|
||||
style="color: #1d2124; text-decoration: none;"> {{ sec.title }}</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<li class="list-group-item" style="background: #23418A; color: #f5f5f5">
|
||||
<a href="{% url "front:main_list" main=main_section.code %}"
|
||||
@@ -57,10 +89,11 @@
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="list-group-item">
|
||||
<a href="{% url "front:sub_list" main=main_section.code sub=sec.code %}"
|
||||
<a href="{% url "front:sub_list" main=main_section.code|default:sec.parent.code sub=sec.code %}"
|
||||
style="color: #1d2124; text-decoration: none;"> {{ sec.title }}</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</ul>
|
||||
@@ -121,7 +154,8 @@
|
||||
<!-- 首页 -->
|
||||
{% if articles.has_previous %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page=1">首页</a>
|
||||
<a class="page-link"
|
||||
href="?page=1{% if q %}&q={{ q }}{% endif %}">首页</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="page-item disabled">
|
||||
@@ -132,7 +166,8 @@
|
||||
<!-- 上一页 -->
|
||||
{% if articles.has_previous %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page={{ articles.previous_page_number }}"
|
||||
<a class="page-link"
|
||||
href="?page={{ articles.previous_page_number }}{% if q %}&q={{ q }}{% endif %}"
|
||||
aria-label="Previous">
|
||||
<span>上一页</span>
|
||||
</a>
|
||||
@@ -154,7 +189,7 @@
|
||||
{# 显示规则:第1-2页 | 当前页前后各2页 | 最后2页 #}
|
||||
{% if i == 1 or i == 2 or i == articles.paginator.num_pages or i == articles.paginator.num_pages|add:"-1" or i >= articles.number|add:"-2" and i <= articles.number|add:"2" %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page={{ i }}">{{ i }}</a>
|
||||
<a class="page-link" href="?page={{ i }}{% if q %}&q={{ q }}{% endif %}">{{ i }}</a>
|
||||
</li>
|
||||
{% elif i == articles.number|add:"-3" or i == articles.number|add:"3" %}
|
||||
{# 省略号只出现一次 #}
|
||||
@@ -168,7 +203,9 @@
|
||||
<!-- 下一页 -->
|
||||
{% if articles.has_next %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page={{ articles.next_page_number }}" aria-label="Next">
|
||||
<a class="page-link"
|
||||
href="?page={{ articles.next_page_number }}{% if q %}&q={{ q }}{% endif %}"
|
||||
aria-label="Next">
|
||||
<span>下一页</span>
|
||||
</a>
|
||||
</li>
|
||||
@@ -181,7 +218,8 @@
|
||||
<!-- 尾页 -->
|
||||
{% if articles.has_next %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page={{ articles.paginator.num_pages }}"
|
||||
<a class="page-link"
|
||||
href="?page={{ articles.paginator.num_pages }}{% if q %}&q={{ q }}{% endif %}"
|
||||
aria-label="Last">
|
||||
<span>尾页</span>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user