[Flask practice] To solve configuration problems related to QQ mailbox/mysql, flaskmysql
I learned from the book flask web (Miguel, with the cover of a dog) and intend to implement a tourism website. During this process, I found that some changes need to be made to the flasky blog program in the book:
1. Registration Email:126, qq and other email addresses are required in China. By default, readers have learned how to implement flasky mail. The differences between 126 and QQ mail are as follows:
First, the mailbox PORT ['mail _ port'] = 465Different from gmail's port 587
Second, use the Protocol ['mail _ USE_SSL '] = TrueUnlike gmail's TLS protocol
Third, USERNAME ['mail _ username'] = '2017 *** 81'You only need to provide the user name without the suffix (@ qq.com)
Fourth, the PASSWORD ['mail _ password'] = 'apnrtcqkuq ***** ce'Go to the mailbox carrier to enable the stmp service and obtain the authorization code (not the logon password of the mailbox)
Other configurations are similar to the flasky environment. You can use a separate config file to store security-related information.
2. Change the database:Sqlite used in the book is a lightweight local database with the aim of being easy to learn, but wants to deploy a web program on the Internet for people to access, medium-sized databases with high concurrency and support for remote services, such as MySQL, are recommended to deploy web programs on Sina cloud, because MySQL is free of charge when the computing workload is low. We recommend that you use Sina cloud SAE to deploy web programs. The entire deployment process is friendly and suitable for individual developers and experiment programs, the most important thing is that the amount of cloud space and database computing provided by free users is large enough. The author also recommends a friendly free deployment platform, but it is a pity that VPN access is required in China, and the speed is extremely slow.
The Flask framework is similar to mysql and sqlite. I use the Pycharm development environment in Ubuntu to build the flask project. Pycharm comes with a database visualization plug-in and does not need to use navicat (this tool is also very difficult to configure and has very slow access in China ).
Install MySQL:
sudo apt-get install mysql-server
sudo apt-get
install mysql-client
sudo apt-get
install libmysqlclient-dev
After the installation is complete, the mysql service is enabled. At this time, no matter whether you log on to the mysql root Account or Pycharm from the terminal to access the database, error 1045 is reported because the root user is not set a password, solution and related commands will not go into detail, refer to the http://blog.csdn.net/wwwdc1012/article/details/51745906 need to note: mysql users can create many, database can also create many.
After logging on to mysql with a new root password, you can create users and databases as needed. Commonly used MySQL command reference: http://www.cnblogs.com/penciler/p/4813157.html needs to note that we only need to create a database and access the database user name, password, do not need to create a table in the database, the table should be created by the SQLalchemy statement in the flask program when the web service is started (models class in reference books ).
Install and introduce the flask-MySQL, Flask-MySQLdb, and Flask_SQLalchemy packages in the Flask project. After installation, enter the database URI in the program:
['Sqlalchemy _ DATABASE_URI '] =' mysql: // username: password @ localhost: 3306/database name'
Open the Database plug-in of Pycharm --> connector se --> MySQL, enter the mysql user name, password, and Database name created on the machine, and click Test Connection. The result is displayed successfully, after your visual Database has been configured, the flask program can read and write data in the Database. In the Database plug-in, you can intuitively see tables, primary keys, foreign keys, and specific data.
But I do not know whether it is related to the Pycharm version. My Test Connection button is gray and shows that the driver file MySQL Connector/J ver5.1.40 is missing. The Download link is provided next to it, but there is no response after clicking it. Download the driver file from the MySQL official website, and add the driver to Additional in this window. Restart Pycharm and the Test Connection button to work properly.
So far, the mailbox and database problems used in the actual flask web program have been solved.