Flask Blog actual combat---upload avatar function

Source: Internet
Author: User

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

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.