Django ImageField默认 [英] Django ImageField default

查看:245
本文介绍了Django ImageField默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

models.py:

class UserProfile(models.Model):

    photo = models.ImageField(upload_to = get_upload_file_name,
                              storage = OverwriteStorage(),
                              default = os.path.join(settings.STATIC_ROOT,'images','generic_profile_photo.jpg'),
                              height_field = 'photo_height',
                              width_field = 'photo_width')
    photo_height = models.PositiveIntegerField(blank = True, default = 0)
    photo_width = models.PositiveIntegerField(blank = True, default = 0)

views.py:

def EditProfile(request):

    register_generator()
    source_file = UserProfile.objects.get(user = request.user).photo

    args = {}
    args.update(csrf(request))
    args.update({'source_file' : source_file})

模板中的某处:

{% generateimage 'user_profile:thumbnail' source=source_file %}

我收到一个错误: UserProfile匹配查询不存在.

I receive an error: UserProfile matching query does not exist.

在此行:

source_file = UserProfile.objects.get(user = request.user).photo

问题是ImageField的默认属性无法正常工作.因此,不会在模型内部创建对象.如何正确使用该属性?如果我省略此属性,则创建该对象时不会出错.我需要通过绝对路径还是相对路径? 我正在使用django-imagekit调整图像的大小,然后再将其抹除: http://django-imagekit.readthedocs.org/en/latest/

The problem is that the default atribute of ImageField is not working. Thus, the object is not created inside my model. How to use this atribute properly? If I ommit this atribute, the object is created with no errors. Do I need to pass the absolute or relative path? I am using django-imagekit to resize the image before dispalying it: http://django-imagekit.readthedocs.org/en/latest/

推荐答案

如果未定义默认属性,图像上传是否成功?当我在自己的django项目中实现ImageField时,我没有使用默认属性.相反,我编写了此方法来获取默认图像的路径:

If you don't define the default attribute, does image uploading work successfully? When I implemented an ImageField in my own django project, I didn't use the default attribute. Instead I wrote this method to get the path to the default image:

def image_url(self):
"""
Returns the URL of the image associated with this Object.
If an image hasn't been uploaded yet, it returns a stock image

:returns: str -- the image url

"""
    if self.image and hasattr(self.image, 'url'):
        return self.image.url
    else:
        return '/static/images/sample.jpg'

然后在模板中,使用以下命令显示图像:

Then in the template, display the image with:

<img src="{{ MyObject.image_url }}" alt="MyObject's Image">

简单示例

在views.py

def ExampleView(request):
    profile = UserProfile.objects.get(user = request.user)
    return render(request, 'ExampleTemplate.html', { 'MyObject' : profile } )

然后在模板中添加代码

<img src="{{ MyObject.image_url }}" alt="MyObject's Image">

将显示图像.

也针对错误"UserProfile匹配查询不存在".我假设您已经在UserProfile模型中的某个位置为User模型定义了外键关系,对吗?

Also for the error 'UserProfile matching query does not exist.' I assumee you have defined a foreign key relationship to the User model somewhere in your UserProfile model, correct?

这篇关于Django ImageField默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆