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:45] 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 223: | Línea 231: | ||
| With this you could create other storages like ''PrivateMediaStorage'' and use it like this: | With this you could create other storages like ''PrivateMediaStorage'' and use it like this: | ||
| <code python> | <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> | ||