模型.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')
因此,如果当前 user.username 是John,则个人资料图片将转到uploads/J/John/profile_photo/filename
视图.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)
最后,模板包含:
{% generateimage 'user_profile:thumbnail' source=source_image %}
我的问题是:
source_image 应该是什么?它没有在官方文档中指定。我试图做source = '/static/uploads/J/John/profile_photo/EU.jpg',但后来我得到错误'SafeText' object has no attribute 'name'
如果 source_image 应该是图像的路径,如何动态指定它?谢谢