, And gets all the content, and then encapsulates all the values in the configuration file into the profile template created in the previous stepPrint (App.config.get ("CCC"))========= Way Three: The way of the object ============os.environ[' flaks-settings '] = ' settings.py '=============== mode Four (recommended): The way of the string, convenient operation, not to change the configuration, directly change the string on the line ==============App.config.from_object (' Settings. DevConfig ')--
Recently in the computer bawed research Tools and Applications This course, ASSIGNMENT3 teachers asked themselves to find an open source project to delve into and modify. It's interesting and useful to see developing the web with flask, and it's decided to start from there.First build a virtual environment, using virtualenv can, I am using the Mac computer, terminal input command line sudo easy_install virtualenv can be installed. Create a microblog d
module
1, installation
Pip Install Flask-mail orEasy_install Flask-mailLooking at the source code of the module will find that it is ultimately called by the Smtplib module to implement.
2, configuration
Flask-mail is configured using the Flask standard configuration API. Here are all the configuration options:
M
boot managementPip Install Flask-sqlalchemy # install flask-sqlalchemy to manage the databasePip Install Mysql-python # install mysql-python Drive DatabaseThe next thing to note is that the code for the full functionality of the Flaskblog We now need can be written in a . py file, But I really don't recommend it (and I'm sure no one would recommend it). If this is very inconvenient to extend the functional
sufficient for simple applications.
2. Routing and view FunctionsA client, for example, a web browser, sends requests to the web service and then sends them to the Flask application instance. The application instance needs to know the code to be run for each URL request, so it creates a URLs ing for the Python function. These operations that establish connections between URLs and functions are called routes.
The most convenient way to define a route
, mainly used to store files such as css and js.
_ Init _. py: module initialization file,FlaskThe program object must be created in_ Init _. pyFile, then we can safely import and reference each package.
Setting. py: configuration file, database username and password, etc.
3. develop code
1. run the project first:
1) write the _ init _. py file to create a project object. the code is as follows:
#-*-Coding: UTF-8-*-from flask import
This article mainly introduces the Jinja2 template engine learning tutorial in the Python Flask framework. the usage of Jinja2 template engine is also an important knowledge in Flask Web development, for more information, see the template function of Flask. it is implemented based on the Jinja2 template engine. The template file is stored in the current subdirect
Pydev plugin was successfully installed4. Configure PydevWindows->preferences->pydev->interpreter-python,new a Python interpreter, fill in the Interpreter name and path, and select the appropriate Python.exeAt this point, the Pydev configuration is finishedFlask Project Framework Construction1. Create a new Python projectFile->new->pydev Project2. Create a new project Structure folderapp– root directorystatic– static Resource directory, pictures, JS,CSS, etc.templates– templates _init_. py– in
In the previous code, the view function returned a string, and the content of the HTML file returned in the actual development was actually a string. Below is a summary of several ways in which flask can be used as a response. 1, response string.
This has been explained earlier, but it should be noted that in fact, every time a view function responds with a status code. For example, the status code is 200, indicating that the request has been successf
A module is a file that contains the response text, which contains the dynamic portion of the placeholder variable representation, and its specific value is only known in the context of the request. Replace the variable with the real value and return the resulting response string, which is called rendering. To render the module, Flask uses a powerful template engine called JINJA2.First, JINJA2 template engineThe simplest form of Jinja2 template is a f
powerful way to reuse code is template inheritance, which is similar to class inheritance in Python code. First, create a base template named base.html:
2. Integrate Twitter Bootstrp with Flask-Bootstrap
Bootstrap is a client architecture, so it does not directly involve servers. The server provides HTML responses that reference the Bootstrap Cascading Style Sheet (CSS) and Javascript files, and instantiate required components in HTML/CSS and Javasc
SQLite database. SQLite is the best choice for a small program database, a database that can be stored as a single file.
Add a new configuration item (fileconfig.py) to our configuration file:
Import os
basedir = Os.path.abspath (Os.path.dirname (__file__))
Sqlalchemy_database_uri = ' sqlite:///' + Os.path.join (Basedir, ' app.db ')
Sqlalchemy_migrate_repo = Os.path.join (basedir, ' db_repository ')
Sqlalchemy_database_uri is the necessary extension of the
database. SQLite is the best choice for a small program database, a database that can be stored as a single file.
Add a new configuration entry (fileconfig.py) to our configuration file:
Import Osbasedir = Os.path.abspath (Os.path.dirname (__file__)) Sqlalchemy_database_uri = ' sqlite:///' + os.path.join ( Basedir, ' app.db ') Sqlalchemy_migrate_repo = Os.path.join (basedir, ' db_repository ')
Sqlalchemy_database_uri is the flask-sqlalchemy require
,delete, which is not conducive to the study of SQL Data table structure is difficult to modify-in the design sometimes ignore the definition of some fields, in the ORM mode can only update the Mapping Table Update field, and the effect is far less than the ALTER TABLE [table name] Add field name Update field is easy to understand, often ignore some details But it has to be said that ORM mode does open the door to a new world, greatly reducing the process and difficulty of learning and using da
Flask RoutingBefore explaining what a Flask route is, elaborate on the code for the smallest application of Flask "Hello World".Flask "Hello World"1 fromFlaskImportFlask2 3App = Flask (__name__)4 5@app. Route ('/')6 defHello ():7
# -*- coding: utf-8 -*-
def is_isbn_or_key(word):
isbn_or_key = 'key'
if len(word) == 13 and word.isdigit():
isbn_or_key = 'isbn'
short_word = word.replace('-', '')
if '-' in word and len(short_word) == 10 and short_word.isdigit():
isbn_or_key = 'isbn'
return isbn_or_key
Call this method in the main file, remember to pass the value, and receive the value returned
# -*- coding: utf-8 -*-
from flask
folder:config.pyrequirements.txtrun.pyinstance/ config.pyyourapp/ __init__.py models.py views.py templates/ static/Using the Instance folderTo load the configuration variables defined in the instance folder, you can use the app.config.from_pyfile() . If you set the call Flask() when you create an app instance_relative_config=True , you app.config.from_pyfile() will see a special file in the instance
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.