Flask+Ajax 的模板如何添加按钮,点击后刷新局部页面 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
schema

Flask+Aax 的模板如何添加按钮,点击后刷新局部页面

  •  
  •   schema Aug 26, 2017 7330 views
    This topic created in 3168 days ago, the information mentioned may be changed or developed.

    基本情况: 最近折腾 Flask,学着做了一个小项目练手。会用简单的 html+css,不会 js

    app.py 文件内容如下:

    # app.py import random from flask import Flask, render_template app = Flask(__name__) ### One douban film short comment ### ### random output ### @app.route("/", methods=["GET"]) def index(): return render_template("index.html") @app.route("/douban", methods=['GET']) def Comment(): with open('douban.txt', 'r') as f: lines = f.readlines() num = random.choice(range(len(lines))) return lines[num] if __name__ == "__main__": app.run() 

    模板文件夹 templates 下 index.html 文件内容如下:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>My Flask</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <div id="comment"></div> <script> $.get('/douban').done(function (data) { var para=document.createElement("p"); var node=document.createTextNode(data); para.appendChild(node); var element=document.getElementById("comment"); element.appendChild(para); }); </script> </body> </html> 

    现在可以做到的是,按 F5,首页会重新加载一条豆瓣电影短评,但这样不太方便。

    所以我想如果在页面内添加一个刷新按钮,点击后 局部刷新 <div id="comment"> 会好很多,请问该怎么做呢

    P.S 附上 douban.txt

    11 replies    2017-08-30 15:43:11 +08:00
    111111111111
        1
    111111111111  
       Aug 26, 2017 via Android
    了解一下 ajax
    LeeSeoung
        2
    LeeSeoung  
       Aug 26, 2017
    加一个 button 添加 onclick 事件,发请求,在回调里刷新页面元素。
    schema
        3
    schema  
    OP
       Aug 26, 2017
    @LeeSeoung 能不能写下
    loading
        4
    loading  
       Aug 26, 2017
    《锋利的 jQuery 》,去买一本看吧。
    Kilerd
        5
    Kilerd  
       Aug 26, 2017 via iPhone
    楼上都给出思路了,还想要具体代码?
    pipapa
        6
    pipapa  
       Aug 26, 2017
    前端后端分离,发送 post 请求,异步刷新
    dong3580
        7
    dong3580  
       Aug 26, 2017 via Android
    你都说了是 Ajax,先去了解一下何为 Ajax,
    fatears
        8
    fatears  
       Aug 26, 2017 via iPad
    都引入 jquery 了为什么还用原生 js 来写
    schema
        9
    schema  
    OP
       Aug 26, 2017 via Android
    收藏倒不少了。。。搞不懂
    TJT
        10
    TJT  
       Aug 27, 2017
    套个 iframe,然后 reload 这个 iframe 就好了,简单快捷 (滑稽
    kitar
        11
    kitar  
       Aug 30, 2017
    <button Onclick="newMonitorModal(0)">新建调度</button>

    function newMonitorModal(id_) {
    $('#modal-content').html("");
    $.get(
    "{{ url_for('mt.NewMonitor') }}",
    {
    id: id_
    },
    function (response) {
    $('#modal-content').html(response);
    }
    }
    );
    }
    About     Help     Advertise     Blog     API     FAQ     Slana     983 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 58ms UTC 22:17 PVG 06:17 LAX 15:17 JFK 18:17
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86