【问题标题】:Upload Image in the wrong directory in django ImageField在 django ImageField 的错误目录中上传图像
【发布时间】:2019-06-04 11:56:23
【问题描述】:

当我上传照片时,照片加载成功,但是照片放错了目录。

而不是将图像放在'media/posts-pics/' 的路径上——正如我在我的帖子模型中所概述的那样——它被放置在'media' 路径上。

这些是我的文件:

models.py

class Post(models.Model): 
    index_pic = models.ImageField(upload_to='posts-pics/')

Project.urls.py

if settings.DEBUG: 
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

views.py

def add_post(request):
    if request.method == "POST":
        form = AddPostForm(request.POST, request.FILES)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.index_pic = form.cleaned_data['index_pic']
            new_post.save()

            return redirect('view_post')

    else:
        form = AddPostForm()
    template = 'post/add_edit_post.html'
    context = {'form': form}
    return render(request, template, context)


def edit_post(request, slug):
    post = get_object_or_404(Post, slug=slug)

    if request.method == "POST":
        form = AddPostForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            Post.objects.filter(id=post.id).update(title=request.POST['title'],
                                                   index_pic=form.cleaned_data['index_pic'],
                                                   )

            return redirect('view_post')
    else:

        form = AddPostForm(instance=post)
    template = 'post/add_edit_post.html'
    context = {'form': form}
    return render(request, template, context)

我为add_post 使用了完全相同的代码,并且照片在它的位置,但我在edit_post 中遇到了麻烦。 怎么了?

注意:

从技术上讲,我可以删除 'media/post-pics',但这是通过 特殊用途和目的是:每个应用程序都有其文件夹 保存图像。

【问题讨论】:

  • 您可以像这样删除多余的括号:index_pic = models.ImageField(upload_to='posts-pics/')
  • 我删除了括号,但问题没有解决。@Stevy

标签: python django django-2.2 django-uploads


【解决方案1】:

问题是你没有以正确的方式使用你的ModelForm

在 edit_post 视图中,您要替换它:

Post.objects.filter(id=post.id).update(
    title=request.POST['title'],                                                   
    index_pic=form.cleaned_data['index_pic'],
    )

简单明了:

 form.save()

它将负责更新以form.instance 传递的帖子(使用经过清理的数据,您当前的代码并非如此)

FWIW,在你的 add_post 视图中,你也想替换这个

        new_post = form.save(commit=False)
        new_post.index_pic = form.cleaned_data['index_pic']
        new_post.save()

简单明了:

        new_post = form.save()

再次强调,ModelForms 的全部意义在于它们知道如何创建和更新模型实例。

【讨论】:

  • 我遇到了一个特殊情况,这就是我使用更新方法的原因。有些数据不会从用户那里得到。因此,它不会在request.POSTAddPostForm 中创建一个字段来为其提供所需的值。我需要做什么?这些字段的一个简单示例是created_datemodified_date(当然,还有其他类似的字段,用户不应该填写,但由系统填写)。@bruno-desthuilliers
  • 我已将您建议的代码包含在我的应用程序中。除了上面的注释,我遇到的另一个问题是:我保存时,我提到的字段的值被替换为空值。 @bruno-desthuilliers
  • 我在这个 [问题] (stackoverflow.com/questions/56451143/…) @bruno-desthuilliers 中解释了我上面的 2 个 cmets。请检查一下。
  • 这并不是什么“特殊”情况,真的,Django 提供了解决这个问题的方法,方法是在模型级别(通常是创建和最后修改日期等)使某些字段不可编辑,并通过从 ModelForms 中排除某些字段。所有这些都记录在案(在精美手册的模型和表格部分中)。
猜你喜欢
  • 2015-02-22
  • 2018-11-27
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多