Python で Jinja テンプレートを使用して Web アプリを開発し始めました。Jinja を使用すると、Python コードから index.html にオブジェクトを送信できますが、index.html から Python コードにオブジェクトを受信することはできますか? たとえば、リストを前後に渡します。もしそうなら、何か例はありますか?
ありがとう!
Python で Jinja テンプレートを使用して Web アプリを開発し始めました。Jinja を使用すると、Python コードから index.html にオブジェクトを送信できますが、index.html から Python コードにオブジェクトを受信することはできますか? たとえば、リストを前後に渡します。もしそうなら、何か例はありますか?
ありがとう!
Why do this? Any logic that you implement in the template is accessible to you in the controller of your app, including any variables that you place in the template context.
If the data has been changed due to interaction with the user, then the best way to retrieve data, in my opinion, is to set up a form and use the normal POST method to send the request and the required data, correctly encoded and escaped, back to your program. In this way, you are protected from XSS issues, among other inconveniences. I would never do any processing in a template, and only use any local logic to modify the presentation itself.
EDIT Taking into account your scenario, I suggest the following:
If you don't want the intermediate step of caching a key-value pair, you may want to encode the whole list in the GET URL, and the step of sanitising and validation should be as easy on the whole list as on a key to the list. Both methods avoid a round trip to the database, protect you from malicious use, and respect the separation of data, presentation, and logic.
考えただけで.. テンプレートを処理した後、jinja に渡した dict の変数にアクセスしようとしましたか?