简体   繁体   中英

How to save a user’s image and display it

I have a user profile model.

class Profile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(User,default='default.jpg', upload_to='profile_pics',) #default avatar user /download avatar user 
    avatar = models.CharField(max_length=300, null=True, blank=True) #field for storing url from a social network

    def save(self, *args, **kwargs):
        super(Profile, self).save(*args, **kwargs)

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

It has two fields for the image.

1- regular default avatar (input image ) it can be set by the user and after downloading it will be saved in the media / profile_pics folder.

2 field (input avatar ) - to store the avatar url from outside (social network). I also have two authorizations, regular and social ( python-social-auth ). When authorizing a call through social networks, I use the function to get the url of the user address:

def get_avatar(backend, response, user=None, *args, **kwargs):

    if backend.name == 'vk-oauth2': 
        url = response.get('photo', )   
    if url:
        user.profile.avatar = url
        user.profile.save()

I get a link of this kind ( url variable):

https://sun9-12.userapi.com/c852132/v852132234/118b6c/aB0GoDEWzaw.jpg

and save this url user.profile.avatar .

In the template, I output:

<img  src="{{user.profile.image.url }}"> - #default avatar user/download avatar user
<img  src="{{user.profile.avatar}}"> - #social avatar (url) user

How do I make sure that when authorizing through social authorization, the image from the url is downloaded and saved in a folder as for imagefield ( profile.image )? That is, during the atvorization through social networks, the picture was downloaded by url, and it was saved as if this field were image and I was able to access it through user. profile.image.url , not a separate user.profile.avatar field?

I would be grateful for any help.

You could download the image via urllib.request :

import urllib.request
import mimetypes

    def save_image(user):
        url = user.profile.avatar
        filename = user.id + mimettypes.guess_extension(urllib.request.urlopen(url).headers['content-type'])
        path = os.path.join(settings.MEDIA_ROOT, filename)

        urllib.request.urlretrieve(url, path)

        user.image = path
        user.save()

depending on your storage location, you might change the path, but this should be a sketch of a possible solution.

EDIT: As you are using an ImageField, this should also work:

import urllib.request

    def save_image(user):
        url = user.profile.avatar
        file = urllib.request.urlopen(url).read()

        user.image = file
        user.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM