如何将图像添加到django中的帖子中 [英] How to add image into a post in django

查看:182
本文介绍了如何将图像添加到django中的帖子中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个博客系统,允许用户在他们的博客中添加图片。

I'm building an blog system, which allow user add image to their blog.

当用户添加图片时,图像将自动上传,博客是张贴的,所以我应该如何处理上传的图片,这些图像是一种像临时图像,因为如果用户发布博客,这些图像将有一个外键到这个博客,并保存到一些文件夹,但如果用户丢弃博客,这些临时图像应该被删除。

when user add image, the image will be upload automatically, this happened before the blog is posted, so how should I handle the uploaded image, these image is kind of like temporary image, because if the user post the blog, these images will have an foreign key to this blog , and saved into some folder,but if the user discard the blog , these temporary images should be deleted.

问题是如何获取首个上传的图像,当博客实际发布时?我应该在哪里存放这些临时图像?我如何判断用户是否丢弃博客?

the problem is how to get the firstly uploaded images,when the blog is actually posted? where should I store these temporary images? and how can I tell if the user discard the blog?

推荐答案

我会建议如下:


  1. 修改Post模型以添加一个名为的日期时间字段,允许NULL。

  2. 使用 ,以确定该帖子是否已发布。如果已发布的字段为NULL,则将其视为草稿,否则发布。

  3. 在您创建发布按钮后立即创建该帖子。这将给你一个Post对象与 id ,您可以绑定到ModelForm并显示给用户进行编辑。所以当他们添加图片时,您可以上传它,并以任何你想要的方式将其绑定到帖子的 id

  4. 只有当您点击发布按钮时,才将发布的 更改为datetime.now()。

  5. 删除已发布或者草稿应删除所有链接的资源,如图像。

  1. Modify the Post model to add a datetime field called published which allows NULL.
  2. Use the field published to determine if the post is published yet or not. A post will be considered as a draft if the published field has NULL, published otherwise.
  3. Create the post as soon as you hit the create post button. This will give you a Post object with the id which you can bind to a ModelForm and display to the user for editing. So when they add a picture, you can upload it and bind it to the id of the post in whatever way you want.
  4. Change the published to the datetime.now() only when you hit the publish button.
  5. Deleting a published or draft post should delete all of the linked resources like images.

希望它有帮助。

这篇关于如何将图像添加到django中的帖子中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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