简体   繁体   English

如何在django博客文章中添加图像

[英]how to add an image in a django blog post

I'm working on a blog with django and one of the last things I'm trying to figure out is how to post an image on my blog. 我正在使用django开发博客,而我想弄清的最后一件事是如何在博客上发布图片。 I can already post some text very nicely through my admin panel and what I want now is the ability to add an image to the post. 我已经可以通过管理面板很好地发布一些文本,现在我想要的是能够在帖子中添加图像的功能。 I'm a complete newbie so excuse any basic mistakes. 我是一个新手,所以请原谅任何基本错误。

I thought of a model like this: 我想到了这样的模型:

class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=255)
    description = models.CharField(max_length=255)
    content = models.TextField()
    published = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to='media')

    class Meta:
        ordering = ['-created']

    def __str__(self):
        return ('%s') % self.title

    def get_absolute_url(self):
        return reverse('blog.views.post', args=[self.slug])

Although when I try this I get an error 'no such column blog_post.image', even after doing manage.py sqlflush and re-syncing my db. 尽管当我尝试执行此操作时,即使执行了manage.py sqlflush并重新同步了我的数据库后,我仍然收到错误“没有此类列blog_post.image”。

and I have this view: 我有这种看法:

def index(request):
    posts = Post.objects.filter(published=True)
    return render(request, 'blog/index.html', {'posts': posts})

Basically I would like the option of adding an image to a post and the template to display it. 基本上,我想选择将图像添加到帖子中并使用模板来显示它。

I have tried admin-files and various other apps and tried so many snippets of code from other people's website but just nothing works so it's not for lack of trying. 我尝试了管理文件和其他各种应用程序,并尝试了其他人网站上的许多代码片段,但没有任何效果,因此并非缺乏尝试。 The closest to what I want is found here: http://drumcoder.co.uk/blog/2010/may/12/django-blog-images/ (which also didn't work for me), just that I want it to be part of my Post model. 在这里找到最接近我想要的内容: http : //drumcoder.co.uk/blog/2010/may/12/django-blog-images/ (这对我也不起作用),只是我想要它成为我的Post模型的一部分。

Is there a simple way of doing this? 有一个简单的方法吗? How would the model and template have to look like? 模型和模板的外观如何? Any help would be greatly appreciated, thanks. 任何帮助将不胜感激,谢谢。

Assuming that you have added image after you have done ./manage.py syncdb . 假设您在完成./manage.py syncdb之后添加了image Then either delete the database and syncdb again (not prefered). 然后要么删除数据库,然后再次删除syncdb(不推荐)。 Or add Django South for data migration (prefered). 或添加Django South进行数据迁移(首选)。

How to work with south? 如何与南方合作? When you have added south in your project. 在项目中添加了South之后。 Remove the image field first so that your model looks exactly same as it was before. 首先删除image字段,以使模型看起来与以前完全相同。 Then perform these steps: 然后执行以下步骤:

  • python manage.py convert_to_south myapp.blog python manage.py convert_to_south myapp.blog
  • python manage.py migrate myapp.blog 0001 --fake python manage.py migration myapp.blog 0001-假

Now add image field back to your Post model and perform these steps: 现在,将image字段添加回Post模型并执行以下步骤:

  • python manage.py schemamigration myapp.blog --auto python manage.py schemamigration myapp.blog --auto
  • python manage.py migrate myapp.blog python manage.py迁移myapp.blog

On the basis that ImageField instance default to varchar(100) columns , you may be able to get away with simply adding a varchar(100) column called image to your APPNAME_post table. 基于ImageField实例默认为varchar(100)列的基础 ,您只需将名为image的varchar(100)列添加到APPNAME_post表中,就可以摆脱困境。

This could be done using an appropriate GUI (eg: pgAdmin III for PostgreSQL) or via a command-line interface (eg: psql) with a command similar to the following: 可以使用适当的GUI(例如:PostgreSQL的pgAdmin III)或通过命令行界面(例如:psql)使用类似于以下命令的命令来完成此操作:

ALTER TABLE APPNAME_post ADD COLUMN image varchar(100);

Personally, I'd go with Aamir's suggestion of using South. 就我个人而言,我会接受亚米尔使用南方的建议。 It might be more work setting it up, but it will be invaluable for any future schema changes. 设置它可能需要做更多的工作,但是对于将来的任何模式更改而言,它都是无价的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM