2

我正在使用 django-nonrel 在 Google App Engine 中构建一个博客站点,我需要一种在博客文章等中存储和显示图像的方法。

这个想法是有一个上传应用程序来上传特定文章等的图像,然后为 imd src 使用绝对或相对 URL。

我正在使用 django-filetransfers 上传图片(http://www.allbuttonspressed.com/projects/django-filetransfers)。

问题是: 1) 是否有人使用 Google App Engine 和 django-nonrel 来托管他们的博客?如果是这样,您如何以及在哪里存储图像?使用 GAE Blobstore 是不是有点矫枉过正?2) 对于图像 URL,我使用在 fly-transfers 应用程序中设置的下载路径。例如。 这个对吗?不使用 .png 扩展名或任何东西来引用似乎有点奇怪。但这可能是从 blobstore 引用图像的方式吗?

仍在使用 Django 和 Google App Engine 学习绳索,因此非常感谢任何帮助。

谢谢

4

1 回答 1

1

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

于 2012-01-17T01:40:29.117 回答