从Django上传到S3

时间:2018-03-15 21:14:57

标签: html django url amazon-s3

我正在尝试向Django网站添加功能,以将文件上传到AWS S3存储桶。我找到了django-s3direct,这似乎非常合适。但是,在完成所有设置说明之后,我收到了渲染基本HTML模板的错误:

<html>
<head>
    <meta charset="utf-8">
    <title>s3direct</title>
    {{ form.media }}
</head>
<body>
    <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
    </form>
</body>
</html>

我在Reverse for 's3direct' not found. 's3direct' is not a valid view function or pattern name.的行上收到错误{{ form.as_p }}

在我的urls.py中(如s3direct文档中所述):

from django.conf.urls import url
from .views import get_upload_params, generate_aws_v4_signature, MyView
app_name = 's3direct'

urlpatterns = [
    url(r'^$', MyView.as_view(), name='form'),
    url('^get_upload_params/', get_upload_params, name='s3direct'),
    url('^get_aws_v4_signature/', generate_aws_v4_signature, name='s3direct-signing'),
]

forms.py

from django import forms
from s3direct.widgets import S3DirectWidget
class S3DirectUploadForm(forms.Form):
    csv = forms.URLField(widget=S3DirectWidget(dest='csv_destination'))

views.py中(以及来自s3direct的其他一些长函数):

from django.views.generic import FormView
from .forms import S3DirectUploadForm
class MyView(FormView):
    template_name = 'form.html'
    form_class = S3DirectUploadForm

我已经配置了IAM策略并为我的Django设置文件添加了密钥,并为我的S3存储桶制定了CORS策略。我正在使用Django 2.0。我坚持这个反向错误,似乎无法弄清楚是什么导致它。

1 个答案:

答案 0 :(得分:0)

根据文档urls.py应包含

urlpatterns = [
 url(r'^s3direct/', include('s3direct.urls')),
 ]