如何获取Flask lr应用程序路径的根路径

在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
如题,当前页面为 "/about" 时,就 active
&a href="#"{% if 当前路径 == "/about" %} class="active"{% endif %}&About&/a&
类似上面这种,当前路径如何获取?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
可以使用request.path
官方文档如下:
In this case the values of the above mentioned attributes would be the following:
/page.html
script_root
/myapplication
http://www.example.com/myapplication/page.html
http://www.example.com/myapplication/page.html?x=y
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
老兄也是用Bootstrap的么
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
javascruot
$(function(){
var href = location.
var x = href.match(/\/[\w\/]+$/);
if(x && x[0]){
$(".side-nav &li").find('a[href="'+x[0]+'"]').parent().addClass("active-nav");
简单又方便...
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。英 [flɑ:sk]
美 [flaesk]
n. [分化] 烧瓶;长颈瓶,细颈瓶;酒瓶,携带瓶
CET6+ TEM4
1. Another theory traces the Late Latin word to a metathesis of Latin vasculum.
flagon, fiasco.
烧瓶来自PIE*plek, 转,编织,词源同ply, complicate, flax. 引申义制陶等容器,陶器,瓶罐。比较cruse, crockery.
flask: [14] English acquired flask via French flasque from medieval Latin flasca, a word of uncertain origin. It occurs widely in the Germanic languages (German has flasche, for instance, and Dutch vlesch, and the related word flasce existed in Old English, although it did not survive into Middle English), but it is not clear whether the medieval Latin word was borrowed from Germanic, or whether the Germanic languages originally got it from a Latin word (Latin vāsculum ‘small vessel’, a diminutive form of vās – whence English vascular, vase, and vessel – has been suggested as a source).The sense ‘gunpowder container’, first recorded in the 16th century, may have been inspired by Italian fiasco (source of English fiasco), which came from a variant medieval Latin form flascō. This also produced English flagon [15].=& , mid-14c., from Medieval Latin flasco "container, bottle," from Late Latin flasconem (nominative flasco) "bottle," which is of uncertain origin. A word common to Germanic and Romanic, but it is unclear whether the Latin or Germanic word is the original (or whether both might have got it from the Celts). Those who support a Germanic origin compare Old English flasce "flask, bottle" (which would have become modern English *flash), Old High German flaska, Middle Dutch flasce, German Flasche "bottle." If it is Germanic, the original sense might be "bottle plaited round, case bottle" (compare Old High German flechtan "to weave," Old English fleohtan "to braid, plait"), from Proto-Germanic base *fleh- (see ).
 
 Another theory traces the Late Latin word to a metathesis of Latin vasculum. "The assumption that the word is of Teut[onic] origin is chronologically legitimate, and presents no difficulty exc[ept] the absence of any satisfactory etymology" [OED]. The similar words in Finnish and Slavic are held to be from Germanic.
1. The flask is completely watertight, even when laid on its side.
即使平放,这个真空水杯也是滴水不漏。 来自柯林斯例句 2. There's some sandwiches here and a flask of coffee.
这里有些三明治和一瓶咖啡。 来自柯林斯例句 3. She made sandwiches, filled a flask and put sugar in.
她做了三明治,把保温瓶灌满,又放了糖进去。 来自柯林斯例句 4. The vacuum flask has a strong casing, which won't crack or chip.
这种热水瓶瓶身坚固,不易断裂破碎。 来自柯林斯例句 5. He took a swig of whisky from his hip flask.
他猛灌了一口扁酒瓶中的威士忌. 来自《简明英汉词典》处理web表单表单需要Flask-WTF
在根目录创建一个配置文件
myblog/config.py
CSRF_ENABLED=True
SECRET_KEY='you-will-never-guess'
CSRF_ENABLED配置是为了激活跨站点请求伪造保护
SECRET_KEY是当CSRF激活后,建立一个加密令牌,用于验证表单
修改app/__init__.py
from flask import Flask
app=Flask(__name__)
app.config.from_object('config')
from app import views
#读取配置文件
编写第一个表单
app/forms.py
from flask.ext.wtf import Form
from wtforms import StringField,BooleanField
from wtforms.validators import DataRequired
class LoginForm(Form):
openid = StringField('openid',validators=[DataRequired()])
remember_me = BooleanField('remember_me',default=False)
导入所需的模块
创建一个Form类
定义一个openid文本框(StringField),名字是openid,需要验证是否为空validators=[DataRequired()]
定义一个remember_me选择框(BooleanField ),名字remember_me,默认是False
创建表单模板
app/templates/login.html
{% extends "base.html" %}
{% block content %}
&h1&Sigh In&/h1&
&form method="post" action="" name="login"&
{{form.hidden_tag()}}
Please enter your OpenID:&br&
{{form.openid(size=80)}}&br&
&p&{{form.remember_me}} Remember Me&/p&
&p&&input type="submit" value="Sign In"&&/p&
{% endblock %}
{{form.hidden_tag()}} 用来实现在配置中的CSRF保护,如果已经激活CSRF,这个字段要出现在所有表单中
{{form.openid(size=80)}} form表单的openid字段,输入框大小80
修改 app/views.py
from flask import render_template
from app import app
from .forms import LoginForm
@app.route('/')
@app.route('/index')
def index ():
user={'nickname':'Bob'}
{'author':{'nickname':'John'},
'body':'Beautiful day in Portland!'},
{'author':{'nickname':'Susan'},
'body':'The Avengers movie was so cool!'}
return render_template("index.html",
title="Home",
user=user,
posts=posts)
@app.route('/login',methods=['GET','POST'])
def login ():
form = LoginForm()
return render_template("login.html",
title = "Sign In",
form = form)
#导入LoginForm,视图接受GET和POST请求
#实例化一个LoginForm
接收表单数据
修改 app/views.py
from flask import render_template,flash,redirect
from app import app
from .forms import LoginForm
@app.route('/')
@app.route('/index')
def index ():
user={'nickname':'Bob'}
{'author':{'nickname':'John'},
'body':'Beautiful day in Portland!'},
{'author':{'nickname':'Susan'},
'body':'The Avengers movie was so cool!'}
return render_template("index.html",
title="Home",
user=user,
posts=posts)
@app.route('/login',methods=['GET','POST'])
def login ():
form = LoginForm()
if form.validate_on_submit():
flash('login requested for OpenID="'+form.openid.data+'",remember_me='+str(form.remember_me.data))
return redirect('/index')
return render_template("login.html",
title = "Sign In",
form = form)
#form.validate_on_submit()判断是否提交
#提交后用flash( )传递数据
#redirect( )是url跳转
修改 app/templates/base.html
{% if title %}
&title&{{title}} - myblog&/title&
{% else %}
&title&Welcome - myblog&/title&
{% endif %}
&div&MyBlog:&a href="/index"&Home&/a&&/div&
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
&li&{{ message }}&/li&
{% endfor %}
{% endif %}
{% endwith %}
{% block content %}
{% endblock%}
修改 app/templates/login.html
{% extends "base.html" %}
{% block content %}
&h1&Sigh In&/h1&
&form method="post" action="" name="login"&
{{form.hidden_tag()}}
Please enter your OpenID:&br&
{{form.openid(size=80)}}&br&
{% for error in form.openid.errors %}
&span style="color:"&[{{ error }}]&/span&
{% endfor %}&br&
&p&{{form.remember_me}} Remember Me&/p&
&p&&input type="submit" value="Sign In"&&/p&
{% endblock %}
不输入直接提交会显示错误,这个是程序自带的错误提示,就是form.openid.errors
在输入框输入bob,选中remember_me
在index页面显示出提交的内容
Flask学习总结笔记(5)-- Form表单
Form表单是Web应用中最基础的一部分。为了能处理Form表单,Flask-WTF扩展提供了良好的支持。
Flask-WTF的安装在前面的博客Flask学习总结笔记(1)– ...
Flask Web开发入门(九)之表单处理
本章我们介绍Flask Web开发中的表单处理
application/json类型请求
前台请求代码:
Python Flask-表单提交方式
这篇文章讲两种表单提交方式,先说一下目录树,下图左侧templates文件夹放置html文件,
static文件夹放置css,js文件.1.请求上下文首先在templates文件夹新建一个login...
初识flask——表单()
本文大段代码文字来自于实验楼flask开发轻博客课程,只供学习不作任何商业用途,特此声明。...
flask-表单提交 弹窗 跳转页面
在用flask的过程中,遇到一个问题,就是想提交一个表单之后弹出一个提交成功,然后跳转到另一个页面.
但是如果用submit 则无法跳转,用button不能提交.
最后还是选择了用submit...
Flask-WTF(http://pythonhosted.org/Flask-WTF/)扩展可以把处理Web 表单的过程变成一种愉悦的体验。
以下演示一个简单的Web表单,包含一个文本字段...
request.form 获取POST请求中提交的表单数据
Flask 请求的对象提供的信息足够用于处理web表单,但是人物单调,重复
例如:生成表单的HTML代码和验证提交的表单数据Flask-...
最终展示图:平台:win32 ,
pycharm, python2.7本文主要使用分为三个部分:
1.监视器(monitor.py): 使用python三方模块psutil,每秒获取当前系统cpu...
1. 创建表单
使用Flask-WTF时,每个web表单是由继承自Form/FlaskForm类的子类来展现的。该类在表单中定义了一组表单域,每个都表示为一个对象。每个表单域都可以连接到一个或多个...
之前提到的请求上下文中的request.form可以获得POST请求中提交表单数据,一个个去处理显然很繁琐,这时可以用Flask-WTF来让一切变得更加简单,它集成了WTForms,安装如...
没有更多推荐了,新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
丰衣足食, 积分 652, 距离下一级还需 348 积分
论坛徽章:0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/')
def index():
& & return '&a href=&{}&&favicon.ico&/a&'.format(url_for('static', filename='favicon.ico'))
@app.route('/favicon.ico')
def favicon():
& & return redirect(url_for('static', filename='favicon.ico'), code=301)
if __name__ == '__main__':
& & app.run(debug=True, host='127.0.0.1', port=8080)
运行上面的代码,然后你在其根目录创建一个 static 的目录,放入图片文件,最后你访问下
请问,我建立了& &g:\static\favicon.ico&&,如何让&&可以访问到它?
白手起家, 积分 14, 距离下一级还需 186 积分
论坛徽章:0
本帖最后由 halfcrazy 于
01:38 编辑
@app.route('/favicon.ico')
@app.route('/static')
def favicon():
& & return redirect(url_for('static', filename='favicon.ico'), code=301)
from flask import send_from_directory
@app.route('/static')
def static():
& & return send_from_directory(os.path.join(app.root_path, 'static'),
& && && && && && && && && && & 'favicon.ico', mimetype='image/vnd.microsoft.icon')
你可以看下这里}

我要回帖

更多关于 应用程序的物理路径 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信