fix: trade_policy 宏使用 with context 修复 500

Jinja imported macros did not receive trade_policy from render context, causing Internal Server Error on all instance pages.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-05 00:36:06 +08:00
parent c0ad50a7b5
commit efe7a57e60
9 changed files with 46 additions and 8 deletions
+1 -1
View File
@@ -377,7 +377,7 @@
<button type="submit">手动划转</button> <button type="submit">手动划转</button>
</form> </form>
<form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}"> <form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'order-symbol') }} {{ trade_policy_symbol('symbol', 'order-symbol') }}
{{ trade_policy_direction('direction', 'order-direction') }} {{ trade_policy_direction('direction', 'order-direction') }}
<select id="sltp-mode" name="sltp_mode"> <select id="sltp-mode" name="sltp_mode">
+1 -1
View File
@@ -357,7 +357,7 @@
</div> </div>
{% include 'order_monitor_rule_tips_gate.html' %} {% include 'order_monitor_rule_tips_gate.html' %}
<form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}"> <form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'order-symbol') }} {{ trade_policy_symbol('symbol', 'order-symbol') }}
{{ trade_policy_direction('direction', 'order-direction') }} {{ trade_policy_direction('direction', 'order-direction') }}
<select id="sltp-mode" name="sltp_mode"> <select id="sltp-mode" name="sltp_mode">
+1 -1
View File
@@ -386,7 +386,7 @@
<button type="submit">手动划转</button> <button type="submit">手动划转</button>
</form> </form>
<form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}"> <form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'order-symbol') }} {{ trade_policy_symbol('symbol', 'order-symbol') }}
{{ trade_policy_direction('direction', 'order-direction') }} {{ trade_policy_direction('direction', 'order-direction') }}
<select id="sltp-mode" name="sltp_mode"> <select id="sltp-mode" name="sltp_mode">
@@ -34,7 +34,7 @@
</div> </div>
{% include order_rule_tips_tpl %} {% include order_rule_tips_tpl %}
<form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}"> <form id="add-order-form" action="/add_order" method="post" class="form-row" data-risk-percent="{{ risk_percent }}">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'order-symbol') }} {{ trade_policy_symbol('symbol', 'order-symbol') }}
{{ trade_policy_direction('direction', 'order-direction') }} {{ trade_policy_direction('direction', 'order-direction') }}
<select id="sltp-mode" name="sltp_mode"> <select id="sltp-mode" name="sltp_mode">
+1 -1
View File
@@ -34,7 +34,7 @@
</div> </div>
<div class="row" style="margin-top:10px"> <div class="row" style="margin-top:10px">
<label>币种</label> <label>币种</label>
{% from 'trade_policy_fields.html' import trade_policy_symbol %} {% from 'trade_policy_fields.html' import trade_policy_symbol with context %}
{{ trade_policy_symbol('symbol', 'symbol-input', default_symbol, placeholder='BTC/USDT') }} {{ trade_policy_symbol('symbol', 'symbol-input', default_symbol, placeholder='BTC/USDT') }}
<label>关键位</label> <label>关键位</label>
<select id="key-id"> <select id="key-id">
@@ -142,7 +142,7 @@
{% endif %} {% endif %}
</div> </div>
<form id="key-form" action="/add_key" method="post" class="form-row"> <form id="key-form" action="/add_key" method="post" class="form-row">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'key-symbol') }} {{ trade_policy_symbol('symbol', 'key-symbol') }}
<select name="type" id="key-type-select" required> <select name="type" id="key-type-select" required>
{% if position_sizing_mode != 'full_margin' %} {% if position_sizing_mode != 'full_margin' %}
@@ -24,7 +24,7 @@
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<form id="trend-pullback-form" action="{{ url_for('preview_trend_pullback') }}" method="post" class="form-row"> <form id="trend-pullback-form" action="{{ url_for('preview_trend_pullback') }}" method="post" class="form-row">
{% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction %} {% from 'trade_policy_fields.html' import trade_policy_symbol, trade_policy_direction with context %}
{{ trade_policy_symbol('symbol', 'trend-symbol', placeholder='BTC 或 ETH/USDT') }} {{ trade_policy_symbol('symbol', 'trend-symbol', placeholder='BTC 或 ETH/USDT') }}
{{ trade_policy_direction('direction', 'trend-direction') }} {{ trade_policy_direction('direction', 'trend-direction') }}
<input name="leverage" type="number" min="1" step="1" placeholder="杠杆(必填)" required> <input name="leverage" type="number" min="1" step="1" placeholder="杠杆(必填)" required>
@@ -1,4 +1,7 @@
{# 方向 / 币种:env 账户级限制(三所共用宏) #} {# 方向 / 币种:env 账户级限制(三所共用宏);调用方须 with context #}
{% if trade_policy is not defined %}
{% set trade_policy = {'symbol_restrict_enabled': false, 'direction_restrict_enabled': false, 'symbol_whitelist': [], 'allows_long': true, 'allows_short': true, 'direction_mode': 'both', 'badge_text': ''} %}
{% endif %}
{% macro trade_policy_symbol(name, id, value='', required=true, placeholder='BTC 或 BTC/USDT') -%} {% macro trade_policy_symbol(name, id, value='', required=true, placeholder='BTC 或 BTC/USDT') -%}
{% if trade_policy.symbol_restrict_enabled and trade_policy.symbol_whitelist %} {% if trade_policy.symbol_restrict_enabled and trade_policy.symbol_whitelist %}
<select name="{{ name }}" id="{{ id }}" {% if required %}required{% endif %} class="trade-policy-symbol-select"> <select name="{{ name }}" id="{{ id }}" {% if required %}required{% endif %} class="trade-policy-symbol-select">
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Diagnose page render errors (run inside instance dir with venv)."""
from __future__ import annotations
import os
import sys
import traceback
def main() -> int:
inst = sys.argv[1] if len(sys.argv) > 1 else "crypto_monitor_binance"
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(os.path.join(root, inst))
sys.path.insert(0, os.getcwd())
from app import app # noqa: WPS433
paths = ["/trade", "/key_monitor", "/strategy", "/login"]
with app.test_client() as client:
with client.session_transaction() as sess:
sess["logged_in"] = True
for path in paths:
try:
resp = client.get(path)
print(f"{inst} {path} -> {resp.status_code}")
if resp.status_code >= 400:
body = resp.get_data(as_text=True)
print(body[:3000])
except Exception:
print(f"{inst} {path} -> EXCEPTION")
traceback.print_exc()
return 0
if __name__ == "__main__":
raise SystemExit(main())