使用 Django-nonrel 和文件传输应用程序上传到 blobstore 的图像

发布于 2024-11-18 11:37:29 字数 655 浏览 3 评论 0原文

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

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

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

问题是: 1) 有人使用 Google App Engine 和 django-nonrel 来托管他们的博客吗?如果是这样,您如何以及在哪里存储图像?使用 GAE Blobstore 来实现此用途是否有点过头了? 2) 对于图像 URL,我使用在 flie-transfers 应用程序中设置的下载路径。例如。 这是正确的吗?不使用 .png 扩展名或其他内容进行引用似乎有点奇怪。但这可能是从 blobstore 引用图像的方法吗?

仍在学习 Django 和 Google App Engine,因此我们将不胜感激。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小瓶盖 2024-11-25 11:37:29

可以,

我在使用 Blobstore 和 Django-nonrel 时也有过类似的经历。

对于你的第一个问题,Blobstore 并不过分,我认为实际上这是您无需更新整个项目并重新发布即可上传图像的唯一方法。由于服务器的高复制性和安全性,GAE 不允许您写入目录。这是一种权衡,能够随着需求的增加自动启动所有服务器。如果您尝试执行任何涉及写入目录的操作,App Engine 将会出错。

我自己正在为你的第二个问题寻找更好的解决方案。我希望能够自己通过名称引用该文件。我认为关键是向“上传”模型添加一个额外的属性,该属性在保存时设置为文件名。我没有尝试过,但应该可以。


更新:

这有效。

这是模型:

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)

这是下载处理程序:

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

URL 映射:

url(r'^file/(?P<filename>.+)

执行此操作后,您可以按文件名访问文件(这只是示例应用程序的一个片段)。正如您所看到的,“pk”已替换为“文件名”属性:

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

我自己所坚持的是让“public_download_url”与 GAE Blobstore 一起使用。如果其他人可以评论如何获得适当的公众支持来自动生成公共 URL,我将不胜感激。

授予

, 'cms.views.download_handler_filename', name='cms-download_file'),

执行此操作后,您可以按文件名访问文件(这只是示例应用程序的一个片段)。正如您所看到的,“pk”已替换为“文件名”属性:


我自己所坚持的是让“public_download_url”与 GAE Blobstore 一起使用。如果其他人可以评论如何获得适当的公众支持来自动生成公共 URL,我将不胜感激。

授予

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

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

, '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:


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文