0
각 사용자가 하나의 프로필 사진을 업로드하고 100x100 픽셀로 변환 할 수있게하려고합니다.Google App Engine : Blobstore를 통해 사용자 프로필 사진 업로드 및 변환
내가 ProfilePhoto 모델을 만든:
class ProfilePhoto(ndb.Model):
user_key = ndb.KeyProperty()
blob_key = ndb.BlobKeyProperty()
serving_url = ndb.StringProperty()
created = ndb.DateTimeProperty(auto_now_add = True)
UploadHandler : 다음
class ProfilePhotoForm(BaseHandler):
def get(self, user_id):
user_id = int(user_id)
if is_author:
author_key = ndb.Key('Account', user_id)
profile_photo = ProfilePhoto.query(ProfilePhoto.user_key == author_key).fetch()
if profile_photo:
photo_blob_key = profile_photo[0].blob_key
else:
photo_blob_key = None
upload_url = blobstore.create_upload_url('/%s/upload' % user_id)
user = User.get_by_id(int(user_id))
self.render(
'profile-photo-form.html',
user = user,
upload_url = upload_url,
photo_blob_key = photo_blob_key,
profile_photo = profile_photo)
else:
self.write('You are not author.')
def post(self, user_id):
user_id = int(user_id)
user = User.get_by_id(user_id)
user.profile_photo = self.request.POST.get('profile_photo').file.read()
user.put()
self.redirect('/u/%s' % user_id)
을 UploadHandler 및 ServeHandler은 다음과 같습니다
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self, user_id):
user_key = ndb.Key('User', user_id)
photo = self.get_uploads('profile_photo')
if photo:
existing_photos = ProfilePhoto.query(ProfilePhoto.user_key == user_key).fetch()
if existing_photos:
# Deleting previous user photos if they exist
for e in existing_photos:
blobstore.delete(e.blob_key)
e.key.delete()
photo_info = photo[0]
serving_url = images.get_serving_url(photo_info.key())
profile_photo = ProfilePhoto(
user_key = ndb.Key('User', user_id),
blob_key = photo_info.key(),
serving_url = serving_url)
profile_photo.put()
#self.redirect('/profile-photo/%s' % photo_info.key())
self.redirect('/u/%s' % user_id)
else:
self.redirect('/u/%s' % user_id)
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
blob_info = blobstore.BlobInfo.get(blob_key)
self.send_blob(blob_info)
그리고 마지막으로, 양식을 다음과 같이 표시됩니다.
<form action="{{upload_url}}" method="post" enctype="multipart/form-data">
<label>Profile Photo<br> <input type="file" name="profile_photo"></label>
{% if profile_photo %}
<h4 class="sub-title">Current Photo</h4>
<img src="/profile-photo/{{photo_blob_key}}" />
{% endif %}
<input type="Submit" value="Save">
</form>
이미지는 현재이 같은 내 템플릿에 출력된다 :
<img src="/profile-photo/{{photo_blob_key}}" />
그래서 지금은 나의 템플릿 이미지와 출력을 변환하는 가장 좋은 방법은 get_serving_url()
방법을 사용하는 것입니다 수집하지만, 한 이 점을 나는 그것을 사용하는 방법에 대해 매우 혼란스러워한다. 템플릿에서
을 크기 조정을 넣어? 또는 치수 만 필요하면 치수를 편집하십시오. get_serving_url에는 인수와 인수가 필요합니다. –
네, @ 지미 케인. 나는 그들을 재조정하고 싶다. 내 설정을 감안할 때'get_serving_url()'을 어떻게 사용하겠습니까? (나는 그 질문을 편집했다) – puoyaahhh