开发者

Using images uploaded to the blobstore using Django-nonrel and file-transfer application

I am building a blog site in Google App Engine, using django-nonrel and I need a way to store and display images in blog posts etc.

The idea is to have an upload application to upload images for specific articles etc, and then use an absolute or relative URL for the imd src.

I am using django-f开发者_运维知识库iletransfers to upload the images (http://www.allbuttonspressed.com/projects/django-filetransfers).

Questions are: 1) Is anyone using Google App Engine and django-nonrel to host their blog? If so how and where are you storing images? Is using GAE Blobstore for this use an overkill? 2) For image URL I am using the download path as set up in flie-transfers application. eg. Is this correct? Seems a bit weird not to reference using .png extension or anything. But this might be the way to reference images from the blobstore?

Still learning the rope with Django and Google App Engine so any help would be really appreciated.

Thanks


Can,

I have had a similar experience in using the Blobstore with Django-nonrel.

For your first question, the Blobstore is not overkill and I think is in fact the only way you can upload an image without updating your whole project and republishing it. GAE does not let you write to a directory because of the server's high replication and security. It's a trade off with being able to spin up all the servers automatically as demand increase. If you try to do anything that involves writing to a directory, App Engine will error.

I am looking for a better solution to your second question myself. I would like to be able to reference the file by name myself. The key I think will be adding an extra attribute to the "Upload" Model that gets set to the filename at save time. I have not tried it but it should work.


Update:

This worked.

Here is the Model:

class UploadModel(models.Model):
    title = models.CharField(max_length=64, blank=True)
    file = models.FileField(upload_to='uploads/%Y/%m/%d/%H/%M/%S/')
    filename = models.CharField(max_length=100, editable=False, null=True, blank=True)

    def save(self, *args, **kwargs):
        self.filename = self.file.name.rsplit('/', 1)[-1]
        super(UploadModel, self).save(*args, **kwargs)

here is the download handler:

def download_handler(request, filename):
    upload = get_object_or_404(UploadModel, filename=filename)
    return serve_file(request, upload.file, save_as=True)

the URL mapping:

url(r'^file/(?P<filename>.+)$', 'cms.views.download_handler_filename', name='cms-download_file'),

Once you do this you can access the file by filename (This is just a snippet from the example app). As you can see the 'pk' was replaced with the 'filename' attribute:

{% url cms-download_file filename=upload.filename as fallback_url %}
<p><img src="{% firstof upload.file|public_download_url fallback_url %}"></p>

What I am stuck on myself is getting 'public_download_url' to work with GAE Blobstore. If someone else can comment in with how to get a proper public backed to work that automatically generates the public URL I would greatly appreciate it.

Grant

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜