fix: 添加动态分类页面路由和模板
修复问题:
- 新增类别的详情页面路由不存在
- 导航栏缺少后台入口
新增:
- /category/{id} 路由
- category.html 模板页面
- 导航栏添加后台入口
This commit is contained in:
11
app.py
11
app.py
@@ -75,6 +75,17 @@ def admin_page():
|
|||||||
"""后台管理页面"""
|
"""后台管理页面"""
|
||||||
return render_template('admin.html')
|
return render_template('admin.html')
|
||||||
|
|
||||||
|
@app.route('/category/<category_id>')
|
||||||
|
def category_page(category_id):
|
||||||
|
"""动态分类页面"""
|
||||||
|
categories = load_data(CATEGORIES_FILE)
|
||||||
|
category = next((c for c in categories if c['id'] == category_id), None)
|
||||||
|
|
||||||
|
if not category:
|
||||||
|
return "分类不存在", 404
|
||||||
|
|
||||||
|
return render_template('category.html', category=category)
|
||||||
|
|
||||||
# ============ API路由 ============
|
# ============ API路由 ============
|
||||||
|
|
||||||
@app.route('/api/models')
|
@app.route('/api/models')
|
||||||
|
|||||||
@@ -38,5 +38,14 @@
|
|||||||
"color": "teal",
|
"color": "teal",
|
||||||
"description": "笔记本电脑、台式机参数",
|
"description": "笔记本电脑、台式机参数",
|
||||||
"order": 5
|
"order": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "021dc76d36be",
|
||||||
|
"name": "汽车",
|
||||||
|
"icon": "ri-folder-line",
|
||||||
|
"color": "blue",
|
||||||
|
"order": 6,
|
||||||
|
"description": "汽车方面",
|
||||||
|
"created_at": "2026-04-09 10:09:01"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
11
data/items_021dc76d36be.json
Normal file
11
data/items_021dc76d36be.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "比亚迪宋plus dmi 2021款",
|
||||||
|
"brand": "比亚迪",
|
||||||
|
"price": "18.87",
|
||||||
|
"year": "2021",
|
||||||
|
"category_id": "021dc76d36be",
|
||||||
|
"id": "3d20dbcd4bdd",
|
||||||
|
"created_at": "2026-04-09 10:09:56"
|
||||||
|
}
|
||||||
|
]
|
||||||
170
templates/category.html
Normal file
170
templates/category.html
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{ category.name }} - ParamHub</title>
|
||||||
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body class="bg-gray-50 min-h-screen">
|
||||||
|
<!-- 导航栏 -->
|
||||||
|
<nav class="bg-white shadow-sm sticky top-0 z-50">
|
||||||
|
<div class="max-w-7xl mx-auto px-4 py-3 flex justify-between items-center">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<a href="/" class="flex items-center gap-2">
|
||||||
|
<i class="ri-dashboard-3-line text-2xl text-indigo-600"></i>
|
||||||
|
<span class="text-xl font-bold text-gray-800">ParamHub</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="flex gap-6 text-sm">
|
||||||
|
<a href="/" class="text-gray-600 hover:text-indigo-600">首页</a>
|
||||||
|
<a href="/models" class="text-gray-600 hover:text-indigo-600">模型</a>
|
||||||
|
<a href="/gpus" class="text-gray-600 hover:text-indigo-600">GPU</a>
|
||||||
|
<a href="/cpus" class="text-gray-600 hover:text-indigo-600">CPU</a>
|
||||||
|
<a href="/tools" class="text-gray-600 hover:text-indigo-600">工具</a>
|
||||||
|
<a href="/compare" class="text-gray-600 hover:text-indigo-600">对比</a>
|
||||||
|
<a href="/knowledge" class="text-gray-600 hover:text-indigo-600">知识库</a>
|
||||||
|
<a href="/admin" class="text-gray-600 hover:text-indigo-600">后台</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<!-- 主内容 -->
|
||||||
|
<main class="max-w-7xl mx-auto px-4 py-8">
|
||||||
|
<!-- 分类标题 -->
|
||||||
|
<div class="bg-white rounded-xl shadow-sm p-6 mb-6">
|
||||||
|
<div class="flex items-center gap-4">
|
||||||
|
<div class="w-16 h-16 rounded-xl bg-indigo-100 flex items-center justify-center">
|
||||||
|
<i class="{{ category.icon or 'ri-folder-line' }} text-3xl text-indigo-600"></i>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<h1 class="text-2xl font-bold text-gray-800">{{ category.name }}</h1>
|
||||||
|
<p class="text-gray-500 mt-1">{{ category.description or '参数数据' }}</p>
|
||||||
|
</div>
|
||||||
|
<div class="ml-auto">
|
||||||
|
<span class="text-sm text-gray-400">共 <span id="itemCount">0</span> 条数据</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 搜索和筛选 -->
|
||||||
|
<div class="bg-white rounded-xl shadow-sm p-4 mb-6">
|
||||||
|
<div class="flex gap-4">
|
||||||
|
<div class="flex-1 relative">
|
||||||
|
<i class="ri-search-line absolute left-4 top-1/2 -translate-y-1/2 text-gray-400"></i>
|
||||||
|
<input type="text" id="searchInput" placeholder="搜索..."
|
||||||
|
class="w-full pl-12 pr-4 py-2 border border-gray-200 rounded-lg focus:outline-none focus:border-indigo-400"
|
||||||
|
onkeyup="filterItems()">
|
||||||
|
</div>
|
||||||
|
<select id="sortSelect" onchange="sortItems()" class="px-4 py-2 border border-gray-200 rounded-lg focus:outline-none">
|
||||||
|
<option value="name">按名称</option>
|
||||||
|
<option value="created_at">按时间</option>
|
||||||
|
</select>
|
||||||
|
<a href="/admin" class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition flex items-center gap-2">
|
||||||
|
<i class="ri-add-line"></i> 添加数据
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 数据列表 -->
|
||||||
|
<div class="bg-white rounded-xl shadow-sm p-6">
|
||||||
|
<div id="itemsList" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||||
|
<div class="text-center text-gray-400 py-8">加载中...</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<!-- 页脚 -->
|
||||||
|
<footer class="bg-white border-t mt-8 py-6 text-center text-gray-500 text-sm">
|
||||||
|
ParamHub - 参数百科
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const categoryId = '{{ category.id }}';
|
||||||
|
let allItems = [];
|
||||||
|
|
||||||
|
// 加载数据
|
||||||
|
async function loadItems() {
|
||||||
|
const res = await fetch(`/api/items/${categoryId}`);
|
||||||
|
allItems = await res.json();
|
||||||
|
|
||||||
|
document.getElementById('itemCount').textContent = allItems.length;
|
||||||
|
renderItems(allItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 渲染数据
|
||||||
|
function renderItems(items) {
|
||||||
|
if (items.length === 0) {
|
||||||
|
document.getElementById('itemsList').innerHTML = `
|
||||||
|
<div class="col-span-3 text-center py-12">
|
||||||
|
<i class="ri-inbox-line text-4xl text-gray-300 mb-4"></i>
|
||||||
|
<p class="text-gray-400">暂无数据</p>
|
||||||
|
<a href="/admin" class="mt-4 inline-block px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700">
|
||||||
|
前往后台添加
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('itemsList').innerHTML = items.map(item => {
|
||||||
|
// 动态生成字段显示
|
||||||
|
const fields = Object.entries(item)
|
||||||
|
.filter(([key, val]) => !['id', 'category_id', 'created_at', 'updated_at'].includes(key) && val)
|
||||||
|
.slice(0, 5)
|
||||||
|
.map(([key, val]) => `<span class="text-gray-500 text-sm">${key}: ${val}</span>`)
|
||||||
|
.join('<br>');
|
||||||
|
|
||||||
|
return `
|
||||||
|
<div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition group">
|
||||||
|
<div class="flex items-start justify-between">
|
||||||
|
<div>
|
||||||
|
<h3 class="font-medium text-gray-800 group-hover:text-indigo-600">${item.name || item.title || '未命名'}</h3>
|
||||||
|
<div class="mt-2 space-y-1">
|
||||||
|
${fields}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="text-xs text-gray-400">
|
||||||
|
${item.created_at ? item.created_at.split(' ')[0] : ''}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 搜索过滤
|
||||||
|
function filterItems() {
|
||||||
|
const keyword = document.getElementById('searchInput').value.trim().toLowerCase();
|
||||||
|
if (!keyword) {
|
||||||
|
renderItems(allItems);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filtered = allItems.filter(item => {
|
||||||
|
return Object.values(item).some(val =>
|
||||||
|
String(val).toLowerCase().includes(keyword)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
renderItems(filtered);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 排序
|
||||||
|
function sortItems() {
|
||||||
|
const sortBy = document.getElementById('sortSelect').value;
|
||||||
|
const sorted = [...allItems].sort((a, b) => {
|
||||||
|
if (sortBy === 'name') {
|
||||||
|
return (a.name || a.title || '').localeCompare(b.name || b.title || '');
|
||||||
|
} else {
|
||||||
|
return (b.created_at || '').localeCompare(a.created_at || '');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
renderItems(sorted);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化
|
||||||
|
loadItems();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
<a href="/tools" class="text-gray-600 hover:text-indigo-600">工具</a>
|
<a href="/tools" class="text-gray-600 hover:text-indigo-600">工具</a>
|
||||||
<a href="/compare" class="text-gray-600 hover:text-indigo-600">对比</a>
|
<a href="/compare" class="text-gray-600 hover:text-indigo-600">对比</a>
|
||||||
<a href="/knowledge" class="text-gray-600 hover:text-indigo-600">知识库</a>
|
<a href="/knowledge" class="text-gray-600 hover:text-indigo-600">知识库</a>
|
||||||
|
<a href="/admin" class="text-gray-600 hover:text-indigo-600">后台</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
Reference in New Issue
Block a user