First step: Add a user picture to the form in the database models.py in the field
The second step: Add the Avatar upload in the form of editing the material,
Avatar = Filefield (' Avatar ')
The third step: in the editing data routing interface, add user submit Avatar function, refer to the blog http://ncitycode.com/python/flask/2016/6/18/
Upload_folder = current_app.config[' Upload_folder ']
Here I set the file upload path in config.py to
Change the path of the Avatar store in the database to
Here, upload avatar function is completed, can be uploaded directly through the page avatar
Fourth Step:
If you only completed the above three steps, upload avatar can be successful, the database will also have path data, but the interface will be error, because your front-end page, no changes to display
Here I modified in the user.html display if not uploaded avatar, will display a default avatar, upload the avatar directly display their avatar.
There are some other pages also need to make the same changes, here do not point out, to test it yourself! But there is a page different, that is the _post.html page,
There is a need to add a judgment to whether the blogger has an avatar.
Here, the function is basically complete, toss it together! Sao years.
Project Source:
Https://github.com/627886474/Flask
Flask Blog actual combat---upload avatar function