Flask

Render HTML tags from variable without escaping [duplicate]

你离开我真会死。 提交于 2021-02-19 02:10:43
问题 This question already has answers here : Passing HTML to template using Flask/Jinja2 (6 answers) Closed 5 years ago . I have some HTML content that I want to pass to the template to render. However, it escapes the tags to use HTML entities ( < ), so they show up as code rather than markup. How can I render the html passed to the template? tags = """<p>some text here</p>""" render_template ('index.html',tags=tags) {{ tags }} '< some text here >' I want a paragraph with the text though. some

python主流框架简介和Django框架的使用

倖福魔咒の 提交于 2021-02-19 01:51:53
[TOC] 一、手撸简易web框架 通过统一思想,对各个功能或者接口进行统一,越来越像是Django框架的流程,实现了低配版的Django。下面是具体的流程图 具体步骤为:wsgiref库拿到用户的请求内容,我们判断用户请求的链接地址内容,匹配到对应的后端函数,后端函数内部调用数据库得到需要的数据,再拿到html文件进行读取后,之后再把数据通过jinja2模块语法插入html文件中,最后再把修改过后的html文件return出去。wsgiref库把该html文件内容发送回浏览器,浏览器拿到内容后展示在页面上。结束。 基于第三方模块帮你撸 使用了wsgiref库和jinja2库 wsgiref库帮我们封装了http协议,并将http的请求头和请求首行变成字典。即将请求内容解包,响应的内容压包。 jinja2库专门用来处理后端数据与html页面的交互 views.py 里面的放的是函数 我们管这些函数叫视图函数 视图层 urls.py 里面放的是 路由(后缀)与视图函数的对应关系 路由层 templates文件夹 里面放的全是html文件 模板层 二、动静态网页 1. 静态网页 数据是写死的 万年不变(哪怕改变了 也是人为直接修改) 2. 动态网页 数据是实时获取的 eg: 1 后端代码动态获取 当前时间 2 数据是从数据库查询出来的 三、jinja2模板语法 1. jinja2的作用

jinja2 load template from string: TypeError: no loader for this environment specified

醉酒当歌 提交于 2021-02-18 19:53:30
问题 I'm using Jinja2 in Flask. I want to render a template from a string. I tried the following 2 methods: rtemplate = jinja2.Environment().from_string(myString) data = rtemplate.render(**data) and rtemplate = jinja2.Template(myString) data = rtemplate.render(**data) However both methods return: TypeError: no loader for this environment specified I checked the manual and this url: https://gist.github.com/wrunk/1317933 However nowhere is specified to select a loader when using a string. 回答1: You

jinja2 load template from string: TypeError: no loader for this environment specified

耗尽温柔 提交于 2021-02-18 19:52:25
问题 I'm using Jinja2 in Flask. I want to render a template from a string. I tried the following 2 methods: rtemplate = jinja2.Environment().from_string(myString) data = rtemplate.render(**data) and rtemplate = jinja2.Template(myString) data = rtemplate.render(**data) However both methods return: TypeError: no loader for this environment specified I checked the manual and this url: https://gist.github.com/wrunk/1317933 However nowhere is specified to select a loader when using a string. 回答1: You

Flask WTF form not updating with the sqlite3 database

空扰寡人 提交于 2021-02-18 18:09:52
问题 I have a RadioField form item that uses values for its field from the database. If the database is not created I cannot start the Flask Webserver even though it should only access the database when I go to that page and load that form. If I update the database while the Webserver is running I don't see the form with the new database information. Until I restart the Flask webserver. How can I get it so that it forces the form to reload its values from the database when you visit the page. Also

use many submit buttons in the same form

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-18 12:58:08
问题 I cannot access all the buttons from the frame. It is works only with the Histogram button.Here is my form which I want to access it in the Post method. <form id="package_form" action="" method="post"> <div class="panel-body"> <input type ="submit" name="Download" value="Download"> </div> <div class="panel-body"> <input type ="submit" name="Histogram" value="Histogram"> </div> <div class="panel-body"> <input type ="submit" name="Search" value="Search"> </div> </form> Here is my python code.

Flask Docker container SocketIO Issues

纵饮孤独 提交于 2021-02-18 12:18:06
问题 I have a Flask application that uses SocketIO to fetch data from Postgres live. The app works fine when I run this locally. The problem arouses when I use docker-compose to host my Flask app. My JS client and flask server is hosted into a single app and on the same container. My socketio in JS is like this: var socket = io().connect(window.location.protocol + '//' + document.domain + ':' + location.port); Dockerfile: # Using python 3.7 in Alpine FROM python:3.6.5-stretch # Set the working

Flask file upload limit

强颜欢笑 提交于 2021-02-18 11:54:12
问题 I have a file upload handler for multiple file uploads, and have set the MAX_CONTENT_SIZE. The docs mention that Flask throws a 413 exception when the total file size exceeds the limit, so I've also written a 413 error handler with a custom 413 page. However, when testing the file upload, I can see that the 413 error is definitely thrown, but the connection seems to break everytime instead of rendering my error page. FYI, I'm using the Flask dev server currently. Code: app.config['MAX_CONTENT

How to mock `current_user` in flask templates?

女生的网名这么多〃 提交于 2021-02-18 11:36:54
问题 I want to mock flask-login 's current_user under the template rendering. This function return the current logged user. Right now I'm mocking the AnnonymousUserMixin from flask-login which is returned by default if the user is not authenticated. But this leads to all kind of juggles. If I could simply mock current_user I would be able to create a Mocked object for it to return. Here a sample of what I'm using today: import unnittest from flask_login.mixins import AnonymousUserMixin class

Error launching Flask app with error “Failed to find Flask application”

半世苍凉 提交于 2021-02-18 10:41:08
问题 I'm new to Flask. To launch the flask app I did python -m flask run but I'm repeatedly getting an error Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one. I am using a virtualenv on a Windows 10 machine from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello, World!" if __name__ == '__main__': app.run(debug=True) I would expect a web server to start and to be able to navigate to a local page and view "Hello,