How to change to a customized local avatar? Is there any implemented code? Please give me a link. Thank you
Local image address: ./static/avatar/1.jpg-9.jpg
My code is like this:
def gravatar(self, size=100, default='identicon', rating='g'):
import random
return '%d.jpg' % random.randint(1, 9)
The call is like this:
<img class="img-rounded profile-thumbnail" src="{{ url_for('static', filename='avatar/') }}{{ current_user.avatar_hash } }">
The picture cannot be loaded after I modified it like this. Why?
Welcome to communicate, I am also learning Flask, but I didn’t use the gavatar example in the book, so I don’t know where your problem lies...
Regarding local avatars, I have a semi-finished product written by myself for reference, which implements ajax asynchronous uploading of avatars to the server.
u{id}.jpgUser
Aavatar
field is added to the model to store the url path corresponding to the avatar routing, by accessing the avatar The route obtains the avatar file. The uploaded avatar is stored in/static/img/avatr/n/
according to the user'sid
. EachAVATARS_PER_FOLDER
avatar file is stored in/static/img/avatr/n/
, and the file name is.
I can’t explain it in detail due to time constraints, welcome to communicate~
:
Handle avatar upload and return avatar
html page -
app/templates/user_settings.html:
Handle avatar upload and update display through ajax
The localResizeIMG plug-in is used here to compress the image, get the base64 encoding of the image, and pass in the
upload_avatar()🎜 function to upload asynchronously. If the upload is successful, the avatar display is updated🎜