From 1ad01649f299f5bbe380c0a2d8242ba971ae8fc4 Mon Sep 17 00:00:00 2001 From: Eric0801 <33929918+Eric0801@users.noreply.github.com> Date: Thu, 23 Oct 2025 11:02:17 +0800 Subject: [PATCH] fix: add None check for strategy not found in result_view - Add validation to check if strategy exists before processing - Return user-friendly error message and redirect to result page - Prevents TypeError: 'NoneType' object is not iterable - Ensures Railway deployment has correct result_view.html template --- main.py | 6 ++++ templates/result_view.html | 59 +++++++++----------------------------- 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/main.py b/main.py index 9f72d18..97cedf2 100644 --- a/main.py +++ b/main.py @@ -553,6 +553,12 @@ def result_view(): curs.execute(sql, (sid, )) data= curs.fetchone() conn.close() + + # Check if strategy exists + if not data: + flash(f'策略 ID {sid} 不存在', 'warning') + return redirect(url_for('result')) + # Processing data data = dict(data) data['role'] = role_map.get(data['role'], data['role']) diff --git a/templates/result_view.html b/templates/result_view.html index 63c76fb..e062339 100644 --- a/templates/result_view.html +++ b/templates/result_view.html @@ -260,7 +260,7 @@ Beta VaR10 R2 - {% if data.role == '最大化效用函數' %} + {% if data.role == '最大化效���函數' %} Gamma {% endif %} @@ -302,7 +302,7 @@
- 🤖 AI 投資建議 + 🤖 LLM 投組分析
@@ -311,35 +311,15 @@
Loading...
-

正在生成AI投資建議...

+

正在生成 LLM 投組分析...

- - -
- - -
- - {% comment %} - - - -
"> - ⚙️ 分析選項 - {% endcomment %} @@ -426,46 +406,35 @@ async function refreshLLMAdvice() { const container = document.getElementById('llm-advice-container'); const button = document.getElementById('refresh-advice'); - const cotToggle = document.getElementById('cot-toggle'); try { // 禁用按鈕 button.disabled = true; button.innerHTML = ' 正在生成...'; - // 獲取CoT開關狀態 - const useCot = cotToggle.checked; - const cotLabel = useCot ? '(使用思考鏈推理)' : ''; - // 顯示loading狀態 container.innerHTML = `
Loading...
-

正在生成AI投資建議${cotLabel}...

+

正在生成 LLM 投組分析...

`; - // 調用API,加上cot參數 - const url = `/api/llm_advice/${strategyId}${useCot ? '?cot=true' : ''}`; + // 調用API,使用 CoT(更詳細的思考過程) + const url = `/api/llm_advice/${strategyId}`; const response = await fetch(url); const result = await response.json(); if (result.success) { // 格式化並顯示建議 const formattedAdvice = formatLLMAdvice(result.advice); - - // 如果啟用CoT,顯示標記 - const cotBadge = result.cot_enabled - ? '🧠 Chain-of-Thought 推理模式
' - : ''; - - container.innerHTML = cotBadge + formattedAdvice; + container.innerHTML = formattedAdvice; } else { container.innerHTML = `
-
⚠️ 無法生成投資建議
+
⚠️ 無法生成 LLM 投組分析

${result.error}

${result.details ? `${result.details}` : ''}
@@ -475,14 +444,14 @@ container.innerHTML = `
❌ 發生錯誤
-

無法連接到投資建議服務,請稍後再試。

+

無法連接到 LLM 投組分析服務,請稍後再試。

錯誤詳情:${error.message}
`; } finally { // 恢復按鈕 button.disabled = false; - button.innerHTML = '🔄 重新生成建議'; + button.innerHTML = '🔄 重新生成分析'; } } @@ -619,7 +588,7 @@ } else { container.innerHTML = `
-
⚠️ 無法生成投資建議
+
⚠️ 無法生成 LLM 投組分析

${result.error}

${result.details ? `${result.details}` : ''}
@@ -629,7 +598,7 @@ container.innerHTML = `
❌ 發生錯誤
-

無法連接到投資建議服務,請稍後再試。

+

無法連接到 LLM 投組分析服務,請稍後再試。

錯誤詳情:${error.message}
`; @@ -640,7 +609,7 @@ } } - // 獲取模式描述 + // 獲取模��描述 function getModeDescription(mode) { const descriptions = { 'simple': '快速分析',