Introduction of Flask
Flask is a Python-implemented WEB development micro-framework. Official website: http://flask.pocoo.org/
Second, Demo
1, Code structure
Copy Code code as follows:
.
├──blog.py
├──static
│├──css
││└──index.css
│├──images
││├──cat.jpg
││└──sheying1229.jpg
│└──js
└──templates
├──index.html
├──login.html
├──regist.html
└──upload.html
5 di
First, we introduce the problem of circular referencing:When a module needs to refer to a class of another module, and another module needs to refer to the module's class, there is a circular reference, and cannot import the class, this time can cut off one of the reference path, add a moduleProject structure:models_sep.py Code: fromFlaskImportFlask fromModelsImportarticle fromExtsImportDBImportConfigapp= Flask (__name__) app.config.from_object (confi
/www/www.a.com/myapp.py//insert the content marked with blue into the uwsgi_www.a.comfile from flask import Flaskapp = Flask (_ name _) @ app. route ('/') def hello_world (): return 'Hello World! 'If _ name _ = '_ main _': app. run ()
"Hello World! "Indicates that the environment has been set up successfully.
PS:
1. To
Python Flask framework simple getting started instance, pythonflask
This article describes the simple usage of the Flask framework in python. Share it with you for your reference. The details are as follows:
Use the simple example code of the Flask framework. If you are learning the Flask framework, you can refer to
4.4 Flask-mailIn the development process, many applications need to notify the user via mail, Flask extension Package flask-mail by wrapping Python built-in Smtplib package, can be used in the Flask program to send mail.The Flask-mail connects to the Simple Mail protocol (ea
1, #pip install Flask_script installation Flask_script Package Flask-script is a Flask extension, for Flask The program adds a command-line parser. flask-script comes with a set of common options, and also supports custom commands. 2, import the Manager in the source code, the code is as follows: From
/bin/activate
Creating a python virtual environment in Windows is simpler. Switch to the dos mode and run the following command:> Cd D:> Virtualenv myvir
After the creation, you will find that there will be an additional myvir directory under the d directory, switch to the directory Scripts directory in DOS mode, and execute> Activate. batTo activate the virtual environment.
After activating the virtual environment, we can officially install Flask in
SqlAlchemyis aPythonof theOrmFramework. There is a flask-sqlalchemy extension in flask , which is convenient to use. 1. Create a sqlalchemy the Model ModuleTo create a models.py module When creating the model, first introduce the extension of Flask-sqlalchemy from Flask Import
communicate with Web applications such as programs written with the Flask framework.
Installation instructions are as follows, be sure to ensure that you have entered the virtual environment and activated:
pip Install UwsgiYou do not need to use sudo because virtualvenv does not have permission requirements. Uploading project Files
The Linux connection tool used by bloggers is Xshell, in a previous blog post http://blog.csdn.net/qq_14908027/article/d
data migrations.Second, the configurationThe database used in the tutorial is the SQLite database. The SQLite database is the most convenient choice for small applications, and each database is stored in a single file.There are many new configuration items that need to be added to the configuration file (file config.py):Import= Os.path.abspath (Os.path.dirname (__file__'sqlite:/// ' ' app.db ' 'db_repository')Sqlalchemy_database_uri is required for flask
1. CSRF Protectionfrom = Flask (__name__) app.config['secret_key' Secret_key string'The app. Config dictionary can be used to store configuration variables for frameworks, extensions, and programs themselves.Use the standard dictionary syntax to add configuration values to the App. Config objectSecret_key configuration variable is a universal keyPS: To enhance s
Site directory is created, to not affect the Python environment in the system, we need to install a Python virtual environment under the test directory: sudo virtualenv venvActivating the virtual environment: source Venv/bin/activateand install Flask:pip install flask in the virtual environment
Then we add two files to the test directory, one is APP.WSGI and the other is hello.py.1, hello.py:
From flask im
Without Nginx, the flask data can be automatically gzip.
I used Flask to do a little thing myself, but the data in it is huge. Every time I get data from the server, I have to download more than 5 MB of data. I don't want to bother with the O M shoes to change the configuration in Nginx, so I found the flask-compress.
How to use it? Easy
First download
migration. PY DB Upgrade|downgrade A
-
- " "
the
-App = Flask (__name__)
-
- #set the URL of the connection database
+app.config['Sqlalchemy_database_uri'] ='Mysql://root:[email protected]:3306/migratetest'
-app.config['sqlalchemy_track_modifications'] =True +
Adb =SQLAlchemy (APP) at
- #migration initialization, the first parameter is the
Use Flask-Migrate to upgrade the management database.
When upgrading the system, we often encounter operations such as updating the data structure on the server. The previous method was to manually write an alter SQL script for processing, and we often find omissions, as a result, the program cannot be used normally after being published to the server.
Now we can use the Flask-Migrate plug-in to solve thi
Tags: field data user name session unique RECT effect ext stepOne, the realization function: Generates the login interface through the Web form, when the input user name is not in the background database, adds in, when, displays happy see you again style; 1. Database Engine: MySQL (install no path required, I was not moved before) 2. Database framework: Flask-sqlalchemy (book recommended, direct pip install Flask
Tutorial on implementing the paging function in the Python Flask framework
This article mainly introduces how to implement the paging function in the Python Flask framework. The example in this article is implemented based on a blog. For more information, see
Submission of Blog Posts
Let's start with something simple. A user must submit a new post form on the homepage.
First, we define a single-domain form
. Please follow this link to activate your account:{{ confirm_url }}Cheers!
Save this as activate.html in "project/templates/usermetadata. This uses a variable called confirm_url, which will be created in the register () view function.
Send email
You can use Flask-Mail installed in project/_ init _. py to create a basic function to send emails.
Create an email. py file:
# project/email.py from flask.ext.mail import Message from project import
This article mainly introduces a simple Getting started example of the Flask framework in python, and analyzes the usage skills of the Flask framework with a hello program, for more information about how to use the Flask framework in python, see the following example. Share it with you for your reference. The details are as follows:
Use the simple example code
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.