Suchergebnisse werden nicht zur Auswahl der "vollständigen Kategorie" mit Flask angezeigtHTML

HTML-Programmierer
Anonymous
 Suchergebnisse werden nicht zur Auswahl der "vollständigen Kategorie" mit Flask angezeigt

Post by Anonymous »

Ich erstelle jetzt eine Suchseite.
Wir haben sogar eine Kategorie -Suchfunktion hinzugefügt. Wenn Sie "vollständige Kategorie" wählen, sollten alle Suchergebnisse herauskommen, aber sie tun es nicht. Wie kann ich es dazu bringen, aufzutauchen? < /P>
Dies ist mein Python -Code.

Code: Select all

from flask import Flask, render_template, request, redirect
import json
import os

app = Flask(__name__)
DATA_PATH = 'data/documents.json'

def load_documents():
with open(DATA_PATH, encoding='utf-8') as f:
return json.load(f)

def save_documents(documents):
with open(DATA_PATH, 'w', encoding='utf-8') as f:
json.dump(documents, f, ensure_ascii=False, indent=4)

def generate_unique_id(documents):
if not documents:
return 1
return max(doc.get('id', 0) for doc in documents) + 1

@app.route("/", methods=["GET", "POST"])
def index():
query = request.args.get("query", "")
category_filter = request.args.get("category_filter", "")
documents = load_documents()
print(f"Loaded documents: {documents}")
results = documents

if category_filter and category_filter != "":
results = [doc for doc in results if doc["category"] == category_filter]

if query:
results = [doc for doc in results if query in doc["title"] or query in doc["content"]]

print(f"Query in template: '{query}'")
print(f"Category Filter in template: '{category_filter}'")
print(f"Results in template: {results}")
return render_template("index.html", query=query, results=results, category_filter=category_filter)

@app.route("/add", methods=["POST"])
def add():
title = request.form["title"]
content = request.form["content"]
image = request.form.get("image", "")
category = request.form.get("category", "미분류")
documents = load_documents()
new_id = generate_unique_id(documents)
new_doc = {"id": new_id, "title": title, "content": content, "image": image, "category": category}
documents.append(new_doc)
save_documents(documents)
return redirect("/")

@app.route("/edit/")
def edit(id):
documents = load_documents()
document = next((doc for doc in documents if doc["id"] == id), None)
if document:
return render_template("edit.html", document=document)
return redirect("/")

@app.route("/update/", methods=["POST"])
def update(id):
title = request.form["title"]
content = request.form["content"]
image = request.form.get("image", "")
category = request.form.get("category", "미분류")
documents = load_documents()
for doc in documents:
if doc["id"] == id:
doc["title"] = title
doc["content"] = content
doc["image"] = image
doc["category"] = category
break
save_documents(documents)
return redirect("/")

def get_all_search_terms():
documents = load_documents()
search_terms = []
for doc in documents:
search_terms.append(doc["title"])
search_terms.append(doc["content"])
return search_terms

if __name__ == "__main__":
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists(DATA_PATH):
save_documents([])
app.run(debug=True, host='127.0.0.1', port=5000)
< /code>
Dies ist mein HTML -Code < /p>




검색 사이트
[*]

.add-form-floating {
position: fixed;
bottom: 20px;
right: 20px;
width: 250px;
background: #ffffffcc;
border: 1px solid #ccc;
padding: 15px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
font-size: 14px;
z-index: 1000;
}

.add-form-floating h3 {
margin-top: 0;
font-size: 16px;
}

.add-form-floating input[type="text"],
.add-form-floating textarea {
width: 100%;
margin-bottom: 8px;
padding: 5px;
box-sizing: border-box;
font-size: 14px;
}

.add-form-floating textarea {
height: 60px;
resize: vertical;
}

.add-form-floating input[type="submit"] {
width: 100%;
background-color: #4CAF50;
color: white;
border: none;
padding: 6px;
border-radius: 4px;
cursor: pointer;
}

.add-form-floating input[type="submit"]:hover {
background-color: #45a049;
}






검색





전체 카테고리
일상
정보
리뷰
질문

카테고리 검색

{% if query or category_filter %}
검색 결과:
{% if results %}
[list]
{% for r in results %}

[b]{{ r.title }}[/b] [{{ r.category }}]

{{ r.content }}

{% if r.image %}
[img]{{ r.image }}[/img]
{% endif %}
[url=/edit/{{ r.id }}]수정[/url]

{% endfor %}
[/list]
{% else %}
검색 결과가 없습니다.
{% endif %}
{% endif %}



bottom: 20px;
right: 20px;
width: 300px;
background: white;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
border-radius: 10px;
padding: 10px;
display: none; /* 처음엔 숨김 */
z-index: 9999;">

문서 추가
제목: 

내용: 

카테고리:

미분류
일상
정보
리뷰
질문


이미지 주소 (선택): 






+



const toggleBtn = document.getElementById('toggle-button');
const form = document.getElementById('floating-form');
let isOpen = false;

toggleBtn.addEventListener('click', () => {
isOpen = !isOpen;
form.style.display = isOpen ? 'block' : 'none';
toggleBtn.textContent = isOpen ? '×' : '+';
});









글 수정




글 수정

제목: 

내용: {{ document.content }}

카테고리:

미분류
일상
정보
리뷰
질문


이미지 주소 (선택): 




[url=/]메인 페이지로 돌아가기[/url]



< /code>
Dies ist mein CSS -Code, < /p>
body {
font-family: sans-serif;
background: #f9f9f9;
padding: 40px;
}
.container {
max-width: 600px;
margin: auto;
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
input[type="text"] {
width: 80%;
padding: 8px;
}
button {
padding: 8px 12px;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px 0;
border-bottom: 1px solid #eee;
}
.edit-button {
padding: 3px 6px; /* 내부 여백을 더 줄임 */
font-size: 0.8em; /* 글자 크기를 더 줄임 */
margin-left: 5px; /* 간격도 조금 줄임 */
}

.category {
font-size: 0.8em;
color: gray;
margin-left: 5px;
}
< /code>
Dies ist mein JSON -Code.  < /p>
[
{
"id": 1,
"title": "파이썬 소개",
"content": "파이썬은 간단하고 강력한 프로그래밍 언어입니다.",
"image": "https://www.python.org/static/community_logos/python-logo.png",
"category": "정보"
},
{
"id": 2,
"title": "인공지능이란?",
"content": "인공지능은 인간의 지능을 모방하는 기술입니다.",
"image": "https://static.vecteezy.com/system/resources/previews/021/059/827/original/chatgpt-logo-chat-gpt-icon-on-white-background-free-vector.jpg",
"category": "정보"
},
{
"id": 3,
"title": "테라리아 리뷰",
"content": "테라리아는 픽셀 게임임에도 불구하고 그래픽이 매우 좋음. 굳",
"image": "",
"category": "리뷰"
},
{
"id": 4,
"title": "얭얭이 반주",
"content": "ㅇㄴㅁㅁㄴㅇ",
"image": "",
"category": "일상"
},
{
"id": 5,
"title": "테라리아 리뷰2",
"content": "망겜",
"image": "",
"category": "리뷰"
}
]

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post