<div class="blogpost-body" id="cnblogs_post_body">
<p><span style="font-size:18pt;"><strong>前言</strong></span></p>
<p>本人在一学期时间实现了Python+Flask+MysqL的web建设,页面具有简单的登录注册发布文章搜索文章等功能。</p>
<div>
这篇文章总结了我最近一段时间的学习成果:使用Flask框架搭建一个web service,并在其中加上一些简单的css,js,html等。在本文中以实际的用户模块为例。之所以写这篇文章是因为想要总结自己一学期的学习成果并且与大家分享。由于是新手如有错漏请见谅。
</div>
<div>
</div>
<div>
<span style="font-size:18pt;"><strong>一、使用工具</strong></span>
</div>
<div>
<strong>python3.5</strong>
</div>
<div>
<strong><strong><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-88850e2b5534db23ee6e0bc1e6cb1317.png"></strong></strong>
<p>实现此页面所有的static文件、templates文件与py文件</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-7f01168d45bb81bc1ae03bfa8d68a881.png"></p>
</div>
<div>
<span style="font-size:18pt;"><strong>二、完成基本的页面设计</strong></span>
</div>
<div>
1.导航条(父模板)
</div>
<div>
<img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-7e3cfb2dd3ed46d4fdbb5c516c728b7d.png">
<p><strong>这里的界面都为继承父模板后的界面.后面将会有相关介绍</strong></p>
<p>2.登录界面</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-1587a520abd83aef9765aacad24af3ae.png"></p>
<p>3.注册界面</p>
</div>
<div>
<img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-5b8a21d1d1ffe9ea0920204ae7da1094.png">
<p>3.发布问答界面</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-4cee9af4dfeeb97e393b738ee6711760.png"></p>
</div>
<div>
<span style="font-size:18pt;"><strong>三、Flask & 概览</strong></span>
<br>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#0000ff;">from</span> flask <span style="color:#0000ff;">import</span><span style="color:#000000;"> Flask
app </span>= Flask(<span style="color:#800080;">__name__</span><span style="color:#000000;">)
@app.route(</span><span style="color:#800000;">'</span><span style="color:#800000;">api/test</span><span style="color:#800000;">'</span><span style="color:#000000;">)
</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> hello():
</span><span style="color:#0000ff;">return</span> <span style="color:#800000;">'</span><span style="color:#800000;">Hello World!</span><span style="color:#800000;">'</span>
<span style="color:#0000ff;">if</span> <span style="color:#800080;">__name__</span> == <span style="color:#800000;">'</span><span style="color:#800000;">__main__</span><span style="color:#800000;">'</span><span style="color:#000000;">:
app.run()</span></pre>
</div>
<p><span style="font-size:18pt;"><strong>四、加载静态文件,父模板与其他界面的继承</strong></span></p>
<p>1.登陆用url_for加载静态文件</p>
<p> 1.<script src="{<!-- -->{ url_for('static',filename='js/login.js') }}"></script></p>
<p> 2.flask 从static文件夹开始寻找</p>
<p> 3.可用于加载css, js, image文件</p>
<p>2.继承和扩展</p>
<p> 1.把一些公共的代码放在父模板中,避免每个模板写同样的内容。base.html</p>
<p> 2.子模板继承父模板</p>
<p> 1.{% extends 'base.html’ %}</p>
<p>3.父模板提前定义好子模板可以实现一些自己需求的位置及名称。block</p>
<p> 1.<title>{% block title %}{% endblock %}-MIS问答平台</title></p>
<p> 2.{% block head %}{% endblock %}</p>
<p> 3.{% block main %}{% endblock %}</p>
<p>4.子模板中写代码实现自己的需求。block</p>
<p> 1.{% block title %}登录{% endblock %}</p>
<p> </p>
<p>以下有如下13个步骤:</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-f756c93193be365c5467004048b2dc36.png"></p>
<p> </p>
<p> </p>
<p><span style="font-size:18pt;"><strong>五、数据库连接池</strong></span></p>
<p>数据库工具:mysql</p>
<p><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-23dd094f28585f929d3af6a3fd19f0ea.png"></p>
<p> </p>
</div>
<p>1.安装与配置python3.6+flask+mysql数据库</p>
<p> 1.下载安装MySQL数据库</p>
<p> 2.下载安装MySQL-python 中间件(pip install flask-sqlalchemy (Python的ORM框架SQLAlchemy))</p>
<p>2.mysql创建数据库</p>
<p>3.数据库配置信息config.py</p>
<div class="cnblogs_code">
<pre class="blockcode">SQLALCHEMY_DATABASE_URI = <span style="color:#800000;">'</span><span style="color:#800000;">mysql+pymysql://root:@localhost:3306/zhuce?charset=utf8</span><span style="color:#800000;">'</span><span style="color:#000000;">
SQLALCHEMY_TRACK_MODIFICATIONS </span>= False</pre>
</div>
<p>4.建立mysql和app的连接</p> |
|