Python 将数据从django上传到AWS S3
我正在从中学习django和AWS 我试图将“dropbox”指向我的桶,但没有成功。我没有文件选择按钮 看起来是这样的: 而应该是: 我错过什么了吗 我正在使用pythonv。3.7.1,Django v 2.1.7。 我试图在许多网站上搜索这个问题,但未能找到解决方案 这是我的密码: 存储_backends.pyPython 将数据从django上传到AWS S3,python,django,amazon-web-services,amazon-s3,Python,Django,Amazon Web Services,Amazon S3,我正在从中学习django和AWS 我试图将“dropbox”指向我的桶,但没有成功。我没有文件选择按钮 看起来是这样的: 而应该是: 我错过什么了吗 我正在使用pythonv。3.7.1,Django v 2.1.7。 我试图在许多网站上搜索这个问题,但未能找到解决方案 这是我的密码: 存储_backends.py class MediaStorage(S3Boto3Storage): location = 'media' file_overwrite = False
class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False
设置.py
os.path.join(BASE_DIR, 'project/static'),
]
AWS_ACCESS_KEY_ID = 'xxx'
AWS_SECRET_ACCESS_KEY = 'xxx'
AWS_STORAGE_BUCKET_NAME = 'xxx'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
DEFAULT_FILE_STORAGE = 'project.storage_backends.MediaStorage'
models.py
class Document(models.Model):
uploaded_at = models.DateTimeField(auto_now_add=True)
upload = models.FileField()
views.py
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .models import Document
class DocumentCreateView(CreateView):
model = Document
fields = ['upload', ]
success_url = reverse_lazy('home')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
documents = Document.objects.all()
context['documents'] = documents
return context
home.html
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
<table>
<thead>
<tr>
<th>Name</th>
<th>Uploaded at</th>
<th>Size</th>
</tr>
</thead>
<tbody>
{% for document in documents %}
<tr>
<td><a href="{{ document.upload.url }}" target="_blank">{{ document.upload.name }}</a></td>
<td>{{ document.uploaded_at }}</td>
<td>{{ document.upload.size|filesizeformat }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3">No data.</td>
</tr>
{% endfor %}
</tbody>
</table>
{%csrf\u令牌%}
{{form.as_p}}
提交
名字
上载于
大小
{文档%中的文档为%}
{{document.uploaded_at}}
{{document.upload.size}filesizeformat}
{%empty%}
没有数据。
{%endfor%}
上面的代码编译得很好