models.py
def get_upload_file_name(instance, filename):
return "uploads/%s/%s/profile_photo/%s" % (instance.user.username[:1], instance.user.username, filename)
class UserProfile(models.Model):
###.....
photo = models.ImageField(upload_to = get_upload_file_name,
blank = True, null = True,
height_field = 'photo_height',
width_field = 'photo_width')
So if the the current user.username is John the profile picture would go to uploads/J/John/profile_photo/filename
views.py :
from .image_processors import register_generator
#.....
@login_required
def EditProfile(request):
register_generator()
#....
image_processors.py
from imagekit import ImageSpec, register
from imagekit.processors import ResizeToFill
class Thumbnail(ImageSpec):
processors = [ResizeToFill(60, 80)]
def register_generator():
register.generator('user_profile:thumbnail', Thumbnail)
Finally, the template contains:
{% generateimage 'user_profile:thumbnail' source=source_image %}
My question is:
what should source_image be? it is not specified in the official documentation. I tried to do source = '/static/uploads/J/John/profile_photo/EU.jpg' , but then I get the error 'SafeText' object has no attribute 'name'
if source_image should be the path to the image, how to dinamically specify it? Thanks
source_image
needs to be a file object. (For example, a_user_profile.photo
.)
It may not be super clear but it is in the docs :
Additional keyword-style arguments are passed to the registered generator class. As we saw above, image spec constructors expect a source keyword argument, so that's what we need to pass to use our thumbnail spec
And earlier :
source_file = open('/path/to/myimage.jpg')
image_generator = Thumbnail(source=source_file)
result = image_generator.generate()
If you can think of some wording that'll make this more clear, please submit a pull request!
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.