"Flask Web Development--a Python-based Web application development practice" Word on-board practice (bottom)

Source: Internet
Author: User
Tags database issues sqlite database



Chapter 8 user authentication

Chapter 9 User Roles

Chapter IV User information

Chapter One blog post

Chapter IV followers

Chapter IV user reviews

Chapter VI application programming Interface


1th-7th Chapter study practice record see: "Flask Web Development--Python-based Web application development practice" Word on-board practice (Part I)

This article records the 8th-14th chapter of the Flask Web development-Python-based Web application development practice. Compared to the beginning to learn 第1-7 chapter content, this part of the content of the actual combat is more powerful, but also in the book encountered a relatively few problems, if carefully understand the first 7 chapters of the content, this part of the content is equivalent to the first 7 chapters of the content of the concrete practice, learning is also very easy.

Let's start by explaining what features are implemented in this part of the project sample.

    • User role: Normal user, Administrator
    • Specific features: login, register, publish articles, edit articles, user concerns, user comments, modify personal information, manage user reviews, markdown display the article format.

As shown in an administrator role log in to each part of the system function picture dynamic diagram:

I'm on my own. Walkthrough Code : (PS: The code is slightly different from the author's GitHub )

Chapter 8 user authentication

1. Book 79 page password hash function Shell test has a bug

If you finish the 7th chapter and write the startup script code in the 71-page manage.py, the manage.py Shell will report the following error in the console input python: TypeError: <flask_script.commands.shell Object at 0x0000000004e29668>: ' Dict ' object was not callable

Solutions such as:

Immediately after that, if the command prompt entered command on the installation book, the following error is reported: Traceback (most recent called last): File "<console>", line 1, in <module>

Solutions See:

2. Book 87 page Insert new user data with Shell bug

This problem is that the database design contains foreign keys, directly according to the book inserted code, not inserted in the user role foreign key, is unable to insert data.

Workaround: Remove the many-to-one relationship between the user table and the role table

Then, delete all the **.sqlite files and Migrations folders in your project, as follows:

Then follow the three steps shown, re-create the database (PS: Here are the methods see Chapter 5th, section 5.11):

After these processing, according to the book insert code, still will not find the user and DB error, the solution is as follows:

After completing the above Data Insertion task, enter Python manage.py runserver on the cmd console, enter Http:// in the browser, and log in with the inserted mailbox number and password. will get the following result:

3. Book 89 pages actual running result (PS: verified, data can be registered successfully)

4. Book 92 pages perform a new database migration

In the cmd window, enter the Python manage.py db migrate-m "The second migration", then the Python manage.py DB Upgrade command, and the results are as follows:

5. Book 94 page example 8-22 code because the bug appears as shown in the Flask-login version issue

Solutions See:

What makes me wonder is that when I write my notes, I look at the physical book, the above code is a problem, the following is the ebook, the above is the correct >~<, as follows:

Problem solution URL (https://segmentfault.com/q/1010000004041497)

The test is_authenticated method is as follows:

Additional Books 95 page run Result:

Reasons for the occurrence:

It took one hours to find the wrong one, and found out that he had lost the return statement when he hit the 83-page example 8-8 code, and wrote User.query.get (int (user_id) directly), as follows:

Solution Source (https://github.com/maxcountryman/flask-login/issues/302):

The successful interface of the operation is as follows:

The following is my own modify the database data in the mailbox validation property to True when the results (PS: Mainly in the mailbox verification needs to request a temporary password, so the specific mailbox data is not filled, directly manually overwrite data in the background database):

Note: The 8th Chapter mainly explains how to use flask framework to realize the user login, registration function, and use the mailbox for user registration confirmation and password modification confirmation. Feeling the 8th chapter is a comprehensive exercise of the contents of the first 7 chapters of the book, all the technical points used can be found in the first 7 chapters. Therefore, the personal feeling as long as the 8th chapter of all the code and the realization of ideas to understand, is just a primer flask framework.

Chapter 9 user Roles

1. Book 99 pages use shell sessions to write roles to database issues

Because in the 8th chapter of the implementation process, I have the role table and the user table between the foreign key relationship to delete, if in this chapter does not re-reply to the relationship between the two tables can not implement the function of the user role. So, in this case, I have to re-initialize, migrate, and create the database. Instead of migrating and creating directly, I made an error in the process of migrating and creating directly: The default property already exists in the framework and cannot create a new role table, which is not resolved due to beginner, helpless, Delete the Migrations folder and the generated Data-dev.sqlite database files directly, re-initialize, migrate and create, and find everything OK. Specific as follows:

Well, since there is a one-to-many foreign key relationship between the role table and the user table, the registration function will have to confirm the user role when inserting the user data, otherwise it cannot be inserted.

The registration test has just been done without having to determine the user role or insert success. This feeling is contrary to my study of the theoretical knowledge of the database Ah, before the 5th chapter, the insertion of data, does not work, now good. It is estimated that the code is a bit of a bug before learning.

Note: This chapter is equivalent to the knowledge of the main database, and the core flask framework of the book's Knowledge point of contact is not very important, the core idea is to define permissions to assign the role level. A user has only one permission, and some can have multiple permissions. The allocation of permissions is very flexible, so that the processing operation greatly simplifies the design of the database, but also improves the data's coherence.

Chapter IV user information

1. Books 105 pages, 107, 109 pages and 112 pages operation results

Here according to the code in the book, to write code operations, the problems encountered in the previous has been encountered, so there is basically no difficulty here. According to the book, the operation results are as follows:

User Profile page

General user modify personal information interface

Administrator modifies personal information interface

Show profile page of user picture

Note: This chapter mainly realizes the personal information editing function of the common user and the Administrator role of the blog system, there is no technical highlights and difficulties, as long as the careful completion of the book, this part of the function is completely free of obstacles.

Chapter One blog post

1.119 Page Example 11-8 code for books

This section of the code on the author GitHub does not have the sample code, that is, if you want to implement the import virtual data test function, you can only one code of one code to knock. However, this is too many points, a letter error is to change the wrong >~<. The code that was cloned directly from the author GitHub, although there is no sample code, but should be the author updated the code, here is another way to implement, because a new fake.py file appears in the project file, the code and example 11-8 code is basically similar.

2. Books 117 pages, 123 pages, 125 pages and 130 pages operation results

Note: This chapter is to expand the function of the blog system, the main implementation of user editing to send a blog post, which can also use markdown to achieve the editing of the article. function specific implementation of reference to the code of the book can be, the basic will not encounter any difficult problems.

Chapter IV followers

1. Books 137 pages and 142 pages actual running results

2. after the 12th chapter of the code is finished, if you select the user to exit, the following bug:attributeerror may appear: ' Anonymoususer ' object has no attribute ' Followed_ Posts '

Workaround, locate the Anonymoususer class in the models.py file, and add a method to the class with the following code:

@property def followed_posts (self):     return Post.query.join (follow, follow.followed_id = = post.author_id         )= = None)

Note: The difficulty of specific implementation in this chapter is to focus on the design of database tables for user records, that is, a multi-pair relationship generates an intermediate table. Other parts, according to the code on the book to operate, basically do not encounter any difficulties.

Chapter IV user reviews

1. Books 148 pages and 151 pages of actual machine running results

Note: This chapter mainly realizes the blog user comment function, as well as adds a wardens the privilege, the concrete realization does not have any difficulty, according to the book's Code to operate the machine.

Chapter VI application programming interface

1. Book 168 pages Test Print results

Here, you need to open the service in CMD before you can get the data in the API. (PS: Recommend a blog: http://blog.csdn.net/huang5487378/article/details/60778293)

Operation Result:

"Flask Web Development--a Python-based Web application development practice" Word on-board practice (bottom)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.