在django中编辑对象时,ImageField不会被填充 [英] When editing an object in django the ImageField is not populated

查看:134
本文介绍了在django中编辑对象时,ImageField不会被填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过表单编辑一个现有的对象。除了ImageField没有填充当前值之外,每件事情都可以正常工作。

I'm trying to edit an existing object through a form. Every thing is working fine except for the ImageField not being populated with the current value.

以下是模型:

class Post(models.Model):
    author = models.ForeignKey(User, editable=False)
    slug = models.SlugField(max_length = 110, editable=False)
    title = models.CharField(verbose_name='Blog Title', max_length=40, blank=False)
    body = models.TextField(verbose_name='Body')
    thumbnail = models.ImageField(upload_to = 'posts/%Y/%m')

这是视图

@login_required
def edit_profile(request, form_class=UserProfileForm, success_url=None, template_name='profiles/edit_profile.html', extra_context=None):

try:
    profile_obj = request.user.get_profile()
except ObjectDoesNotExist:
    return HttpResponseRedirect(reverse('profiles_create_profile'))

if success_url is None:
    success_url = reverse('profiles_profile_detail',
                          kwargs={ 'username': request.user.username })
if form_class is None:
    form_class = utils.get_profile_form()
if request.method == 'POST':
    form = form_class(data=request.POST, files=request.FILES, instance=profile_obj)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(success_url)
else:
    form = form_class(instance=profile_obj)

if extra_context is None:
    extra_context = {}
context = RequestContext(request)
for key, value in extra_context.items():
    context[key] = callable(value) and value() or value

return render_to_response(template_name,
                          { 'form': form,
                            'profile': profile_obj, },
                          context_instance=context)

这里是一个屏幕显示e编辑表单。
http://yfrog.com/jnscreenshot20100110at102jp

And here's a screen show of the editing form. http://yfrog.com/jnscreenshot20100110at102jp

此对象确实附有缩略图,但是当我进行编辑时,缩略图字段中没有显示

This object did have a thumbnail attached but when I went to edit, nothing showed up in the Thumbnail field

推荐答案

我相信答案在于,当文件输入字段显示一个值时,会显示所选用户计算机上文件的本地路径。这个值不会保存在Django的任何地方,所以Django不能也不会在编辑时以该形式显示该值。

I believe the answer lies in the fact that the file input field, when it displays a value, displays the local path to the file on the user's computer that was selected. This value is not saved in Django anywhere so Django can't and won't display this value in the form when editing.

您需要做的是在模板中单独打印每个字段,而不是整体打印表单。然后,使用缩略图字段,将当前选定的缩略图显示为页面上的实际图像,并使用文件输入字段允许用户上传新图像。

What you need to do is print each field individually in the template instead of printing the form as a whole. Then, with your thumbnail field, show the currently selected thumbnail as an actual image on the page and use the file input field to allow the user to upload a new image.

这篇关于在django中编辑对象时,ImageField不会被填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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