Вот создание thread:
@app.route('/create_thread', methods=['POST'])
def create_thread():
forum_id = request.form['forum_id']
title = request.form['title']
content = request.form['content']
# Получить ник текущего пользователя из базы данных
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('SELECT username FROM users WHERE username = ?', (session['username'],))
username = cur.fetchone()[0]
conn.close()
name = translit(title.lower(), 'ru', reversed=True).replace(' ', '-').rstrip('-')
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('INSERT INTO threads (forum_id, name, title, content, author) VALUES (?, ?, ?, ?, ?)', (forum_id, name, title, content, username))
conn.commit()
conn.close()
# Извлечь данные из базы данных
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('SELECT * FROM threads WHERE name = ?', (name,))
current_thread = cur.fetchone()
conn.close()
# Извлечь ответы из базы данных
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('SELECT * FROM answers WHERE thread_id = ?', (current_thread[0],))
answers = cur.fetchall()
conn.close()
# Извлечь данные из базы данных
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('SELECT * FROM threads WHERE name = ?', (name,))
current_thread = cur.fetchone()
conn.close()
# Создать HTML-файл
template_string = '''
<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='css/header_for_threads.css') }}">
<title>{{ title }}</title>
</head>
<body>
{% include "header_for_threads.html" %}
</body>
</html>
'''
html = render_template_string(template_string, title=title, content=content, author=username, username=username, current_thread=current_thread, answers=answers)
# Сохранить HTML-файл
with open(f'templates/threads/{name}.html', 'w', encoding='utf-8') as f:
f.write(html)
# Получить название форума по его ID
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('SELECT name FROM forums WHERE id = ?', (forum_id,))
forum_name = cur.fetchone()[0]
conn.close()
return redirect(url_for('forum', forum_name=forum_name))
Вот обработка написанного сообщения:
@app.route('/thread_message', methods=['POST'])
def thread_message():
thread_id = request.form['thread_id']
answer = request.form['message']
author = session['username']
conn = sqlite3.connect('db.db')
cur = conn.cursor()
cur.execute('INSERT INTO answers (thread_id, content, author) VALUES (?, ?, ?)', (thread_id, answer, author))
conn.commit()
cur.execute('SELECT * FROM answers WHERE thread_id = ?', (thread_id,))
answers = cur.fetchall()
messages = []
for answer in answers:
messages.append({
'author': answer[3],
'content': answer[2]
})
cur.execute('SELECT name FROM threads WHERE id = ?', (thread_id,))
thread_name = cur.fetchone()[0]
conn.close()
return redirect(url_for('thread', thread_name=thread_name))
Вот header_for_threads.html:
<link rel="stylesheet" href="{{ url_for('static', filename='css/header.css') }}">
<container>
<shapka>
<a href="/" class="logo">RSphera</a>
</shapka>
<div style="width: 100%; background-color: white; height: 1px"></div>
<menu>
<a id="menu_forums" style="margin-right: 50px" class="menu_forums">Форумы</a>
<a style="margin-right: 50px" class="menu_forums">Гарант</a>
<a style="margin-right: 50px" class="menu_forums">Реклама</a>
<formarkq>
<marquee scrollamount="25"><a>СЕЙЧАС БЕСПЛАТНАЯ РЕКЛАМА</a></marquee>
</formarkq>
</menu>
</container>
<reklams_menu>
<a href="https://cardbtc.io/"><img src="../static/images/sberbank.jpg"></a>
<a><img src="../static/images/free_mesto.png"></a>
<a href="https://masterbit.pro/"><img src="../static/images/masterbit.gif"></a>
<a><img src="../static/images/free_mesto.png"></a>
<a><img src="../static/images/free_mesto.png"></a>
<a><img src="../static/images/free_mesto.png"></a>
<thread_nameee>
{% for thread in related_threads %}
<th1><a>{{ current_thread[3] }}</a></th1>
{% endfor %}
</thread_nameee>
</reklams_menu>
<container_for_threads>
<info_players>
<avatarrr></avatarrr>
<div><a href="/members/{{ author }}">{{ author }}</a></div>
</info_players>
<content_box>
<div>{{ content }}</div>
</content_box>
</container_for_threads>
<div id="messages-send-container">
{% for answer in answers %}
<container_for_threads>
<info_players>
<avatarrr></avatarrr>
<div><a href="/members/{{ answer[3] }}">{{ answer[3] }}</a></div>
</info_players>
<content_box>
<div>{{ answer[2] }}</div>
</content_box>
</container_for_threads>
{% endfor %}
</div>
<container_for_sendmessage>
<info_players_for_send>
<info_players>
<avatarrr_for_info_send>
<img src="{{ url_for('static', filename='images/avatars/' + session['username'] + '.jpg') }}" alt="Аватар">
</avatarrr_for_info_send>
</info_players>
</info_players_for_send>
<sendy_box>
<form id="send-message-form" action="/thread_message" method="post">
<input type="hidden" name="thread_id" value="{{ current_thread[0] }}">
<input type="hidden" name="thread_name" value="{{ current_thread[3] }}">
<input class="inputmess" type="text" name="message" placeholder="Введите текст">
<input class="inputotprav" type="submit" value="Отправить">
</form>
<div id="answers-container"></div>
</sendy_box>
<script src="../../static/js/threads_answers.js"></script>
</container_for_sendmessage>
При создании thread в main.py он создает новый html файл, который после передает по его ссылке, но он не указывает {{
}} и по итогу он создает пустой:
Как исправить, не знаю(