Question

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. 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.

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.

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.

Était-ce utile?

La solution

Assuming that you have added image after you have done ./manage.py syncdb. Then either delete the database and syncdb again (not prefered). Or add Django South for data migration (prefered).

How to work with south? When you have added south in your project. Remove the image field first so that your model looks exactly same as it was before. Then perform these steps:

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

Now add image field back to your Post model and perform these steps:

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

Autres conseils

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.

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:

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top