Flask Combobox Öğesinin Değerlerine Nasıl Ulaşırım?

Arkadaşlar aşağıdaki şekilde bir HTML ve Flask dosyam var. Ben uygulamadan Link ismi, link, etiket adı ve combobox üstünden girdiğim veriler ile linkleri kaydediyorum. Bunda bir sorun yok. Eklenen etiket de aşağıdaki etiket seç yazan comboya kaydediliyor. Burada da bir problemim yok. Fakat istediğim şu sonradan girilen linkler için eğer renk ve etiket değerleri boş ise aşağıdaki etiket seç yazan combobox üstündeki seçilen etiketin adı ve rengi eklenen yeni linkin adı ve rengi olsun istiyorum.

Sorunu nasıl çözebilirim?

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///links.db'
db = SQLAlchemy(app)

# Veritabanı modelleri
class Link(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    url = db.Column(db.String(200))
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
    tag = db.relationship('Tag', backref=db.backref('links', lazy=True))
    color = db.Column(db.String(20))

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    color = db.Column(db.String(20))

# Veritabanı modellerini oluştur
with app.app_context():
    db.create_all()

@app.route('/')
def home():
    with app.app_context():
        links = Link.query.all()
        tags = Tag.query.all()
        colors = [
            {"name": "Renk Seçiniz", "value": ""},
            {"name": "Kırmızı", "value": "#ff0000"},
            {"name": "Yeşil", "value": "#00ff00"},
            {"name": "Mavi", "value": "#0000ff"},
            {"name": "Sarı", "value": "#ffff00"},
            {"name": "Turuncu", "value": "#ffa500"},
            {"name": "Mor", "value": "#800080"},
            {"name": "Pembe", "value": "#ffc0cb"},
            {"name": "Turkuaz", "value": "#40e0d0"},
            {"name": "Gri", "value": "#808080"},
            {"name": "Siyah", "value": "#000000"},
            {"name": "Beyaz", "value": "#ffffff"},
            {"name": "Sarımsı Yeşil", "value": "#adff2f"}
        ]
        return render_template('index.html', links=links, colors=colors, tags=tags)

@app.route('/add_link', methods=['POST'])
def add_link():
    name = request.form['name']
    url = request.form['url']
    tag_name = request.form['tag']
    color = request.form['colorSelect']

    with app.app_context():
        if not tag_name and color == "Renk Seçiniz":
            tag_name = "Etiket Seçiniz"
            color = "#ffffff"
        else:
            tag = Tag.query.filter_by(name=tag_name).first()
            if not tag:
                tag_name = tag_name  # Değişiklik burada
                tag = Tag(name=tag_name, color=color)
                db.session.add(tag)
            else:
                tag_name = tag.name

        new_link = Link(name=name, url=url, tag=tag, color=color)
        db.session.add(new_link)
        db.session.commit()

        links = Link.query.all()
        tags = Tag.query.all()
        colors = colors = [
            {"name": "Renk Seçiniz", "value": ""},
            {"name": "Kırmızı", "value": "#ff0000"},
            {"name": "Yeşil", "value": "#00ff00"},
            {"name": "Mavi", "value": "#0000ff"},
            {"name": "Sarı", "value": "#ffff00"},
            {"name": "Turuncu", "value": "#ffa500"},
            {"name": "Mor", "value": "#800080"},
            {"name": "Pembe", "value": "#ffc0cb"},
            {"name": "Turkuaz", "value": "#40e0d0"},
            {"name": "Gri", "value": "#808080"},
            {"name": "Siyah", "value": "#000000"},
            {"name": "Beyaz", "value": "#ffffff"},
            {"name": "Sarımsı Yeşil", "value": "#adff2f"}
        ]  # Renk listesi buraya gelecek

        return render_template('index.html', links=links, colors=colors, tags=tags, selected_tag_name=tag_name, selected_color=color)


@app.route('/search', methods=['POST'])
def search():
    query = request.form['query']
    with app.app_context():
        filtered_links = Link.query.filter(Link.name.contains(query)).all()
        tags = Tag.query.all()
        colors = [
            {"name": "Renk Seçiniz", "value": ""},
            {"name": "Kırmızı", "value": "#ff0000"},
            {"name": "Yeşil", "value": "#00ff00"},
            {"name": "Mavi", "value": "#0000ff"},
            {"name": "Sarı", "value": "#ffff00"},
            {"name": "Turuncu", "value": "#ffa500"},
            {"name": "Mor", "value": "#800080"},
            {"name": "Pembe", "value": "#ffc0cb"},
            {"name": "Turkuaz", "value": "#40e0d0"},
            {"name": "Gri", "value": "#808080"},
            {"name": "Siyah", "value": "#000000"},
            {"name": "Beyaz", "value": "#ffffff"},
            {"name": "Sarımsı Yeşil", "value": "#adff2f"}
        ]
        return render_template('index.html', links=filtered_links, colors=colors, tags=tags)

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linkler</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        h1 {
            text-align: center;
        }
        form {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-top: 10px;
        }
        input[type="text"], select {
            width: 100%;
            padding: 5px;
            margin-top: 5px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin-top: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        a {
            text-decoration: none;
            color: #000;
        }
        .search-form {
            margin-top: 20px;
        }
        .tag-search {
            padding: 5px 10px;
            border-radius: 5px;
            font-weight: bold;
            color: #fff;
        }
    </style>
</head>
<body>
    <h1>Linkler</h1>

    <form action="/add_link" method="post">
        <label for="name">Link İsmi:</label>
        <input type="text" id="linkName" name="name">
        <label for="url">URL:</label>
        <input type="text" id="linkUrl" name="url">
        <label for="tag">Etiket:</label>
        <input type="text" id="linkTag" name="tag">
        <label for="colorSelect">Renk Seçin:</label>
        <select id="colorSelect" name="colorSelect" onchange="changeColor()">
            <option value="">Renk Seçiniz</option>
            {% for color in colors %}
                <option value="{{ color['value'] }}">{{ color['name'] }}</option>
            {% endfor %}
        </select>
        <input type="submit" value="Link Ekle">
    </form>
    <label for="tag">Etiket:</label>
    <select id="tag" name="tag">
        <option value="">Etiket Seçiniz</option>
        {% for tag in tags %}
            <option value="{{ tag.id }}">{{ tag.name }}</option>
        {% endfor %}
    </select>

    <h2>Tüm Linkler</h2>
    <table>
        <tr>
            <th>İsim</th>
            <th>Link</th>
            <th>Etiket</th>
        </tr>
        {% for link in links %}
            <tr data-color="{{ link.color }}">
                <td><strong>{{ link.name }}</strong></td>
                <td><a href="{{ link.url }}">{{ link.url }}</a></td>
                <td><span class="tag-search" style="background-color: {{ link.color }};">{{ link.tag.name if link.tag else '' }}</span></td>
            </tr>
        {% endfor %}
    </table>

    <h2>Genel Arama</h2>
    <form class="search-form" action="/search" method="post">
        <label for="query">Arama Sorgusu:</label>
        <input type="text" id="query" name="query">
        <input type="submit" value="Ara">
    </form>
</body>
</html>

Merhabalar,

Yukardaki kısmı şöyle değiştirip deneyin:

    with app.app_context():
        if not tag_name and not color:
            tag = Tag.query.order_by(Tag.id.desc()).first()