0

I'm writing an application using GAE and Django in which I want to give to user the ability to upload his image. Also, I want this image be stored as blob on GAE's datastore. I have seen many examples but nothing specific to this scenario. Although, I feel that is a common issue.

All I want, is to create a new product and this new product must have an image.

1st Attempt: I have tried to add an image attribute (db.BlobProperty()) in product's model, and obviously django does not include it on the presented form.

2nd Attempt: I have created a new entity with two attributes (product as db.ReferenceProperty() and image as db.BlobProperty()). With this I tried to work parallel with django form modifying the django HTML form (including an |input type='file' name='img' /|) expecting that I could take the image from the request object but I failed once more.

This is the Product Class:

class Product(db.Model):
    id = db.IntegerProperty()
    desc = db.StringProperty()
    prodCateg = db.ReferenceProperty(ProductCategory)
    price = db.FloatProperty()
    details = db.StringProperty()
    image = db.BlobProperty()

This is the Django Form (HTML):

<form action="{%url admin.editProduct product.key.id%}" enctype="multipart/form-data" method="post">
<table>
{{form}}
<tr><td><input type="file" name="img" /></td></tr>
<tr><td><input type="submit" value="Create or Edit Product"></td></tr>
</table>
</form>

This is the Django Form (python):

class ProductForm(djangoforms.ModelForm):
  class Meta:
    model = Product
    exclude = ['id']

This is the request handler:

def editProduct(request, product_id):
  user = users.GetCurrentUser()
  #if user is None:
  #  return http.HttpResponseForbidden('You must be signed in to add or edit a gift')

  product = None
  if product_id:
    product = Product.get(db.Key.from_path(Product.kind(), int(product_id)))
    if product is None:
      return http.HttpResponseNotFound('No product exists with that key (%r)' %
                                       product)

  form = ProductForm(data=request.POST or None, instance=product)

  ##########################
  # Ambitious undertaking! #
  ##########################
  #if not product_id:
  #    uploadedImage = get("img")
  #    photo = Image()
  #    photo.product = product
  #    uploadedPhoto = request.FILES['img'].read()
  #    photo.image = db.Blob(uploadedPhoto)
  #    image.put()

  if not request.POST:
    return respond(request, user, 'addprod', {'form': form, 'product': product})

  errors = form.errors
  if not errors:
    try:
      product = form.save(commit=False)
    except ValueError, err:
      errors['__all__'] = unicode(err)
  if errors:
    return respond(request, user, 'addprod', {'form': form, 'product': product})

  product.put()

  return http.HttpResponseRedirect('/product')

As you can see the request handler is based on the Google's Gift-Tutorial

So, If anyone could put his opinion I would be very thankful!

Thank you in advance!

4

1 回答 1

0

您可能想查看一个使用blobstore API 和 blobstoreuploadhandler的示例和/或编辑您的请求处理程序以将上传的文件存储为 blobproperty 或 blobreferenceproperty,具体取决于您使用 blobstore API 还是仅使用 blobproperty 变量。您可以具体说明http post数据,即`

self.request.post('img').file.read() 我确实建议您选择 blobstore API 和 blobstoreuploadhandler,因为它会自动为您做一些非常好的事情:1. 存储 MIM 类型和 2. 存储文件名 3. 通过 get_serving_url 启用服务,这有几个优点。

于 2011-08-25T06:44:37.370 回答