@gndkg

Почему данные не выводятся?

Здравствуйте, есть код:
app.py
from flask import Flask, request, render_template
import mysql.connector
from config import *

app = Flask(__name__)

def get_db_connection():
    conn = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=db_name
    )
    return conn

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/search_results', methods=['GET'])
def search_results():
    query = request.args.get('query', '')
    conn = get_db_connection()
    cursor = conn.cursor(dictionary=True)

    cursor.execute("USE all_info")

    cursor.execute("SHOW COLUMNS FROM events")
    columns = [row['Field'] for row in cursor.fetchall()]

    search_query = "SELECT * FROM events WHERE " + " OR ".join([f"{col} LIKE %s" for col in columns])

    cursor.execute(search_query, [f'%{query}%'] * len(columns))
    results = cursor.fetchall()

    conn.close()

    return render_template('search_results.html', results=results)

if __name__ == '__main__':
    app.run(debug=True)


и на HTML:

index.html
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Поисковик</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 50px;
        }
        input[type="text"] {
            width: 300px;
            padding: 10px;
            margin: 20px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        input[type="submit"] {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <h1>Axtarış</h1>
    <form action="/search_results" method="GET">
        <input type="text" name="query" placeholder="Sorğunuzu daxil edin...">
        <input type="submit" value="Axtar">
    </form>
</body>
</html>


search_results.html
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axtarışın nəticəsi</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 50px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .second-table th {
            background-color: #e6f7ff;
        }
    </style>
</head>
<body>
    <h1>Axtarışın nəticəsi</h1>
    {% if results %}
        <table>
            <thead>
                    <th>Satınalan təşkilatın adı</th>
                    <th>Satınalan təşkilatın VÖEN-i</th>
                    <th>Satınalan təşkilatın ünvanı</th>
                    <th>Musabiqənin adı</th>
                    <th>Müsabiqənin nömrəsi</th>
                    <th>Təsnifat kodu</th>
                    <th>Ehtimal olunan qiymət</th>
                    <th>Müsabiqənin dərc edilmə tarixi və vaxtı</th>
                    <th>Təkliflərin son təqdim olunma tarixi və vaxtı</th>
                    <th>Zərflərin açılış tarixi və vaxtı</th>
                    <th>İştirak haqqı</th>
                    <th>İştirak haqqı təsviri</th>
                    <th>İstifadə haqqı</th>
                    <th>İstifadə haqqı təsviri</th>
                    <th>Ad</th>
                    <th>e-poçt</th>
                    <th>İş vəzifəsi</th>
                    <th>Mobil nömrəsi</th>
                </tr>
            </thead>
            <tbody>
                {% for row in results %}
                    <tr>
                        <td>{{ row['id'] }}</td>
                        <td>{{ row['Organization_name'] }}</td>
                        <td>{{ row['Event_name'] }}</td>
                        <td>{{ row['Event_number'] }}</td>
                        <td>{{ row['Classification_code'] }}</td>
                        <td>{{ row['Suggested_price'] }}</td>
                        <td>{{ row['Event_start_date'] }}</td>
                        <td>{{ row['Submission_deadline'] }}</td>
                        <td>{{ row['Envelope_opening_date'] }}</td>
                        <td>{{ row['Participation_fee'] }}</td>
                        <td>{{ row['Participation_description'] }}</td>
                        <td>{{ row['Usage_fee'] }}</td>
                        <td>{{ row['Usage_description'] }}</td>
                        <td>{{ row['Full_name'] }}</td>
                        <td>{{ row['Contact'] }}</td>
                        <td>{{ row['Position'] }}</td>
                        <td>{{ row['Phone_number'] }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>Sorğunuz üçün heç bir nəticə tapılmadı.</p>
    {% endif %}
    <a href="/">Evə qayıt</a>
</body>
</html>


Данный код Flask должен брать мой запрос, и искать все подходящие данные в подключенной к ней бд. Проблема в том, что когда ввожу запрос, выводит таблицу(но с пустыми рядами) и при этом с ровно тем кол-вом рядов, в которых есть подходящие данные. Как решить данную проблему?
  • Вопрос задан
  • 152 просмотра
Пригласить эксперта
Ответы на вопрос 1
@historydev
Валера, настало твоё время
А, ну раз нужно, тогда тебе на фриланс.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
SPIKS Санкт-Петербург
от 70 000 до 90 000 ₽
move2usajobs.com LLC Лос-Анджелес
от 2 000 до 4 000 $
PERX Нижний Новгород
До 50 000 ₽
28 сент. 2024, в 17:23
10000 руб./за проект
28 сент. 2024, в 16:19
100000 руб./за проект