在 django 中编辑对象时,未填充 ImageField

发布于 2024-08-17 13:57:05 字数 1656 浏览 0 评论 0原文

我正在尝试通过表单编辑现有对象。除了 ImageField 没有填充当前值之外,一切都工作正常。

这是模型:

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)

该对象确实附加了缩略图,但是当我进行编辑时,缩略图字段中没有显示任何内容

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.

Here's the model:

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')

Here's the view

@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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-08-24 13:57:05

我相信答案在于这样一个事实:文件输入字段在显示值时会显示所选用户计算机上文件的本地路径。该值不会保存在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文