[Answered ]-Django + tornado | Image Upload

1👍

Proposal:
User Uploads the image to django, django stores it whenever and return url to it.
The user then sends this url over the websocket, similar to text post.

👤eran

1👍

You should use Django for image upload, it is pretty efficient in doing so. Here is a simple model for image upload.

from django.db import models

LEFT = "left"
RIGHT = "right"
FLOAT_CHOICES = ((LEFT, _("left")),
            (RIGHT, _("right")),
)

class Sample(CMSPlugin):

    float = models.CharField(_("Image placement"), max_length=10, blank=True, null=True,
        choices=FLOAT_CHOICES, help_text=_("Move image left, right or center."))
    image = models.ImageField(_("image"), upload_to=CMSPlugin.get_media_path)
    big_header  = models.TextField(_("Quotation"),null=True, max_length=150)
    name = models.CharField(_("Name of Recommending person"), max_length=150)
    detail = models.CharField(_("School detail"), max_length=200)

In combination with these include two packages in your requirements.txt file:

 boto==2.7.0
 django-storages==1.1.6

These libraries provides very nice support for uploading images to S3(which is what everyone prefer for media files). Once your image is uploaded successfully, it returns an image path which is in turn saved into your database.

In order to use S3, add these settings in your settings.py or base.py.

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

AWS_STORAGE_BUCKET_NAME = '<s3-bucket-name>'
AWS_S3_SECURE_URLS = False
AWS_PRELOAD_METADATA = True
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']

Leave a comment