我正在实施 Boto3 将文件上传到 S3,一切正常。我正在做的过程如下:
我从 FileReader Javascript 对象获取 base64 图像。然后我通过ajax将base64发送到服务器,我解码base64图像并生成一个随机名称来重命名key参数
data = json.loads(message['text'])
dec = base64.b64decode(data['image'])
s3 = boto3.resource('s3')
s3.Bucket('bucket_name').put_object(Key='random_generated_name.png', Body=dec,ContentType='image/png',ACL='public-read')
Run Code Online (Sandbox Code Playgroud)
这工作正常,但尊重性能,有没有更好的方法来改进它?
正如标题所说:我需要在提交表单后呈现模板,此表单FormView由方法处理form_valid.使用这种方法post,我可以在提交之后渲染模板,但也许form_valid,我可以用最干净的方式完成.
我该怎么做?
我有点沮丧试图从表单更新照片。我有这个:
<form action="/subir-fotos/{{campana_id}}/{{point_id}}/" method="POST" enctype="multipart/form-data" id="form_tomaFoto{{page}}">{% csrf_token %}
<input type="hidden" value="{{i.titulo_foto.id}}" name="titulo">
<p><input type="file" accept="image/*" name="foto" required></p>
<p><input type="submit" value="Enviar foto" class="boton"></p>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
我的型号:
class InteraccionFotos(models.Model):
campana = models.ForeignKey('dashboard.Campana')
titulo_foto = models.ForeignKey('actividad_fotos.TitulosFotos')
punto_interaccion = models.ForeignKey(PuntoInteraccion)
foto = models.ImageField(upload_to='.',blank=True,null=True)
Run Code Online (Sandbox Code Playgroud)
然后,在我看来,我有这个:
class SubeFotoView(FormView):
form_class = FilebabyForm
success_url = '/'
template_name = 'tomar_fotos.html'
def form_valid(self,form):
form.save(commit=True)
messages.success(self.request, 'File uploaded!')
return super(SubeFotoView, self).form_valid(form)
def post(self, request, *args, **kwargs):
campana = self.kwargs.get('campana_id')
point_id = self.kwargs.get('point_id')
titulo_foto = request.POST['titulo']
foto = request.FILES['foto']
crea_foto = InteraccionFotos.objects.filter(pk=1,campana_id=3,punto_interaccion_id=5).update(foto=foto) …Run Code Online (Sandbox Code Playgroud)