imagefiled

1
2
3
4
5
6
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.user.id, filename)

class MyModel(models.Model):
image = models.FileField(upload_to=user_directory_path)
  • 1image.url 返回相对路径
  • image.path 返回绝对路径