python_flask简单应用

from flask import Flask,render_template,request
app=Flask(__name__)

@app.route('/')
def index():
	return("hello world")

@app.route('/<string:postn>')
def postget(postn):
	return(postn)

@app.route('/login')
def login():
	return render_template('login.html')

#data.html放在templates目录下
@app.route('/passw',methods=['post'])
def passw():
	name=request.form.get('name')
	passwd=request.form.get('passwd')
	if name=='demo':
		if passwd=='demo':
			return render_template('data.html',name=name)

if __name__=='__main__':
	app.run(port=80)
好的,下面来编写一个基于 Flask 的小网站。这个网站将会有一个主页和一个联系页面。 首先,需要安装 Flask。在命令行输入以下命令: ``` pip install flask ``` 接着,在你的项目文件夹创建一个名为 `app.py` 的 Python 文件,并按照以下步骤进行编写。 第一步,导入 Flask 模块和其他需要的模块。可以使用以下代码: ```python from flask import Flask, render_template, request ``` 第二步,创建 Flask 应用程序实例。使用以下代码: ```python app = Flask(__name__) ``` 第三步,定义主页路由和视图函数。使用以下代码: ```python @app.route('/') def index(): return render_template('index.html') ``` 这个视图函数将会渲染 `index.html` 模板并返回它的内容。 第四步,定义联系页面路由和视图函数。使用以下代码: ```python @app.route('/contact', methods=['GET', 'POST']) def contact(): if request.method == 'POST': email = request.form['email'] message = request.form['message'] # 将电子邮件和消息发送到你的收件箱 return 'Thank you for your message!' else: return render_template('contact.html') ``` 这个视图函数处理 GET 和 POST 请求。如果请求是 GET,它将会渲染 `contact.html` 模板并返回它的内容。如果请求是 POST,它将会从表单获取电子邮件和消息的内容,并将它们发送到你的收件箱。 第五步,编写模板。在你的项目文件夹创建一个名为 `templates` 的文件夹,并在其创建两个名为 `index.html` 和 `contact.html` 的模板文件。 这里提供一个简单的 `index.html` 模板作为示例: ```html <!DOCTYPE html> <html> <head> <title>My Website</title> </head> <body> <h1>Welcome to my website!</h1> <p>Here you can find information about me and my work.</p> </body> </html> ``` 这是一个简单的 `contact.html` 模板作为示例: ```html <!DOCTYPE html> <html> <head> <title>Contact Me</title> </head> <body> <h1>Contact Me</h1> <form method="POST"> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea><br><br> <input type="submit" value="Send"> </form> </body> </html> ``` 第六步,运行应用程序。在命令行输入以下命令: ``` python app.py ``` 这将会运行 Flask 应用程序并在本地主机的默认端口上启动它。在浏览器访问 `http://127.0.0.1:5000/` 将会显示主页,访问 `http://127.0.0.1:5000/contact` 将会显示联系页面。 这就是一个简单的基于 Flask 的网站的示例。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值