Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:django:others [2020/10/10 10:34] alfred [Upload files] |
wiki2:python:django:others [2020/10/31 20:16] (actual) |
||
|---|---|---|---|
| Línea 27: | Línea 27: | ||
| # Delete a session value | # Delete a session value | ||
| del request.session['my_car'] | del request.session['my_car'] | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Sending mails ===== | ||
| + | You can send mails with: | ||
| + | <code> | ||
| + | from django.core.mail import send_mail | ||
| + | send_mail('subject', 'message', 'Dont Reply <do_not_reply@domain.com>', ['youremail@example.com']) | ||
| </code> | </code> | ||
| Línea 192: | Línea 200: | ||
| </code> | </code> | ||
| + | ==== For using S3 or an alterantive service ==== | ||
| + | You need the ''boto3'' and ''django-storages'' packages. | ||
| + | |||
| + | An example of settings would be... | ||
| + | <code> | ||
| + | AWS_ACCESS_KEY_ID = '' | ||
| + | AWS_SECRET_ACCESS_KEY = '' | ||
| + | AWS_STORAGE_BUCKET_NAME = 'codi.coop.test' | ||
| + | AWS_S3_CUSTOM_DOMAIN = f's3.wasabisys.com/{AWS_STORAGE_BUCKET_NAME}' | ||
| + | AWS_S3_ENDPOINT_URL = 'https://s3.wasabisys.com' | ||
| + | AWS_DEFAULT_ACL = 'public-read' | ||
| + | DEFAULT_FILE_STORAGE = 'cc_lib.storages.MediaStorage' | ||
| + | EXTERNAL_MEDIA_PATH = 'fok/media' | ||
| + | MEDIA_FILE_OVERWRITE = True | ||
| + | # AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400',} | ||
| + | </code> | ||
| + | |||
| + | Being the ''cc_lib.storages.MediaStorage'' the next: | ||
| + | <code python> | ||
| + | from storages.backends.s3boto3 import S3Boto3Storage | ||
| + | from django.conf import settings | ||
| + | |||
| + | |||
| + | class MediaStorage(S3Boto3Storage): | ||
| + | location = settings.EXTERNAL_MEDIA_PATH | ||
| + | file_overwrite = settings.MEDIA_FILE_OVERWRITE | ||
| + | </code> | ||
| + | |||
| + | With this you could create other storages like ''PrivateMediaStorage'' and use it like this: | ||
| + | <code python> | ||
| + | class Document(models.Model): | ||
| + | uploaded_at = models.DateTimeField(auto_now_add=True) | ||
| + | upload = models.FileField() | ||
| + | |||
| + | |||
| + | class PrivateDocument(models.Model): | ||
| + | uploaded_at = models.DateTimeField(auto_now_add=True) | ||
| + | upload = models.FileField(storage=PrivateMediaStorage()) | ||
| + | user = models.ForeignKey(User, related_name='documents') | ||
| + | </code> | ||
| + | |||
| + | ==== So... ==== | ||
| + | |||
| + | - Install boto3 and django-storages. | ||
| + | - Create your own MediaStorage. | ||
| + | - Add the settings. | ||
| + | - Configure models. | ||
| + | - Remind that forms in template must have the attribute ''enctype=“multipart/form-data”''. | ||
| + | |||
| + | |||
| + | ==== Notes ==== | ||
| + | <code python> | ||
| + | def upload_path(instance, filename): | ||
| + | if instance.type == Resource.ResourceType.PROFILE_PICTURE.value: | ||
| + | return f'authors/{instance.owner.uuid}/avatars/{instance.uuid}.png' | ||
| + | elif instance.type == Resource.ResourceType.ZINE_COVER.value: | ||
| + | return f'zines/{str(instance.related_to)}/cover.jpg' | ||
| + | </code> | ||