Flask

Flask URL routing : url_for only passes first argument

别等时光非礼了梦想. 提交于 2021-02-11 05:09:49
问题 I have jQuery tabbed panel that loads pages within the tab on click. This works fine. What I want now is to pass additional arguments to the url such as the title of the tab being clicked. The script to load is the following: $(document).ready(function(){ $( "#content" ).load( "{{url_for(xyz, query=query)}}" ); var tabs = $('.tabs > li'); tabs.on("click", function(){ tabs.removeClass('active'); $(this).addClass('active'); $( "#content" ).load( "{{url_for(xyz, query=query)}}" ); }) }); The

Flask - how to get query string parameters into the route parameters

ε祈祈猫儿з 提交于 2021-02-10 20:44:42
问题 Im very much new to Flask, and one of the starting requirements is that i need SEO friendly urls. I have a route, say @app.route('/sales/') @app.route(/sales/<address>) def get_sales(addr): # do some magic here # render template of sales and a simple GET form that submits an address. <form action={{ url_for('get_sales') }}> <input type='text' name='address'> <input type=submit> </form> On form submission, the request goes to /sales/?address=somevalue and not to the standard route. What

Vagrant: Getting connecting clients real ip

别说谁变了你拦得住时间么 提交于 2021-02-10 20:17:38
问题 I have a simple Apache+Flask website running inside a basic Vagrant+VirtualBox environment. I can access my site fine at 127.0.0.1:8080. The question & problem is, how do I configure Vagrant to pass the real clients IP address to Apache+Flask? request.remote_addr always returns 10.0.2.2 no matter what client is connecting from within my LAN. For example the machine running Vagrants IP is 192.168.1.5. From a client i.e. another laptop on my LAN with IP of 192.168.1.7, would hit the site @ 192

Flask 邮件发送

荒凉一梦 提交于 2021-02-10 18:41:02
今天小婷儿给大家分享的是Flask 邮件发送。 Flask 邮件发送 一、Flask 邮件发送 from flask import Flask, render_template, current_app from flask_script import Manager from flask_mail import Mail, Message from threading import Thread app = Flask(__name__) # 配置邮箱服务器 app.config['MAIL_SERVER'] = 'smtp.163.com' # 邮箱用户 app.config['MAIL_USERNAME'] = ' 邮箱 @163.com' # 用户密码 app.config['MAIL_PASSWORD'] = ' 邮箱密码 ' # 创建Mail对象 mail = Mail(app) def async_send_mail(app, msg): # 邮件发送需要在程序上下文中进行, # 新的线程中没有上下文,需要手动创建 with app.app_context(): mail.send(msg) # 封装函数发送邮件 def send_mail(subject, to, template, **kwargs): # 从代理中获取代理的原始对象 app = current

CSRF Protection with Flask/WTForms and React

☆樱花仙子☆ 提交于 2021-02-10 18:10:35
问题 Has anyone successfully implemented CSRF protection for a form submitted with React (as a controlled component) to a Flask back-end (ideally with WTForms)? I've seen a lot of partial answers, and one with Django, but couldn't find anything definitive for Flask. My big issue seems to be that I don't know how to send the csrf token to my react front end, store it as a header before submitting my form, then submit my form with the correct token. Any direction would be really helpful. 回答1: So,

带你认识 flask 错误处理

喜欢而已 提交于 2021-02-10 16:56:51
点击上方蓝字关注我们 欢迎关注我的公众号,志学Python 01 flask 中错误处理机制 在Flask应用中爆发错误时会发生什么?得到答案的最好的方法就是亲身体验一下。启动应用,并确保至少有两个用户注册,以其中一个用户身份登录,打开个人主页并单击“编辑”链接。在个人资料编辑器中,尝试将用户名更改为已经注册的另一个用户的用户名,boom!(爆炸声) 这将带来一个可怕的“Internal Server Error”页面: 如果你查看运行应用的终端会话,将看到 stack trace (堆栈跟踪)。堆栈跟踪在调试错误时非常有用,因为它们显示堆栈中调用的顺序,一直到产生错误的行: (venv) $ flask run * Serving Flask app "microblog" * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) [2017-09-14 22:40:02,027] ERROR in app: Exception on /edit_profile [POST] Traceback (most recent call last): File "/home/miguel/microblog/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py",

Flask的RuntimeError错误处理

最后都变了- 提交于 2021-02-10 16:33:59
在Flask框架中,为我们提供了丰富的上下文对象/变量(request、session、current_app、g),这些上下文对象可以根据不同的上下文环境具备不同的值,所以他们是依赖于上下文环境的,而他们的使用也必须在上下文环境中,如果脱离了上下文环境,对这些没有上下文环境的上下文对象/变量进行相应的操作就会报错:RuntimeError   通俗一点理解(current_app):     应用上下文对象current_app就是对当前Flask创建出来的app对象的一个代理,所以在使用current_app的时候就需要指定它所代理的是哪个app对象(及指定上下文环境),如果未指定,系统就会报错RuntimeError      from flask import Flask, current_app app = Flask( __name__ ) # 错误写法 current_app.config[ ' SECRET_KEY ' ] = ' jfiagiisfashjsjf ' @app.route( " / " ) def demo(): print (current_app.config.get( ' SECRET_KEY ' )) return ' Is OK ' if __name__ == ' __main__ ' : app.run()   如果按照上面的写

Flask / Python / WTForms validation and dynamically set SelectField choices

99封情书 提交于 2021-02-10 16:00:06
问题 I'm trying to create a simple Flask / Python one page web app that uses dynamically created choices for a SelectField. However, I can't get it to POST using dynamically created choices, and there's also some funny validation behaviour going on (will explain after the code) I created a minimum failing example here: from flask import Flask, render_template, flash, redirect from flask_wtf import Form from wtforms import IntegerField, SubmitField, SelectField from wtforms.validators import

Flask / Python / WTForms validation and dynamically set SelectField choices

ε祈祈猫儿з 提交于 2021-02-10 15:59:06
问题 I'm trying to create a simple Flask / Python one page web app that uses dynamically created choices for a SelectField. However, I can't get it to POST using dynamically created choices, and there's also some funny validation behaviour going on (will explain after the code) I created a minimum failing example here: from flask import Flask, render_template, flash, redirect from flask_wtf import Form from wtforms import IntegerField, SubmitField, SelectField from wtforms.validators import

wtforms SelectField with dynamic choices always returns “none” for data

穿精又带淫゛_ 提交于 2021-02-10 15:08:17
问题 I am new to wtforms. I have to provide user with list of fruits and cost of each fruit as shown below, Total number of fruits are dynamically generated and each fruit prices is also dynamically generated. Below is my declaration, from flask.ext.wtf import Form class SelectForm(Form): def __init__(self, csrf_enabled=False, *args, **kwargs): super(SelectForm, self).__init__(csrf_enabled=csrf_enabled, *args, **kwargs) fruits_list = wtforms.FieldList( wtforms.SelectField('fruits', validators =