[英]TypeError when using AWS S3 Bucket with Django
I'm brand new to Django and AWS S3 and seem to have hit a wall.我是 Django 和 AWS S3 的新手,似乎碰壁了。 This is all on localhost at the moment.
目前这一切都在 localhost 上。 I'm trying to add a new product through my own admin app for a Art Gallery website i'm working on, all worked fine originally when adding a product, but since using AWS S3 i receive the following TypeError:
我正在尝试通过我自己的管理应用程序为我正在开发的 Art Gallery 网站添加新产品,最初在添加产品时一切正常,但由于使用 AWS S3,我收到以下 TypeError:
The error states that it's expecting "...a string or bytes-like object."该错误表明它期待“......一个字符串或字节,如 object”。 and refers to the following lines of code in both my mixins.py and my staff views.py
并在我的mixins.py和我的员工views.py中引用以下代码行
mixins.py: mixins.py:
from django.shortcuts import redirect
class StaffUserMixin(object):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_staff:
return redirect("home")
return super(StaffUserMixin, self).dispatch(request, *args, **kwargs)
views.py:视图.py:
class ProductCreateView(LoginRequiredMixin, StaffUserMixin, generic.CreateView):
template_name = 'staff/product_create.html'
form_class = ProductForm
def get_success_url(self):
return reverse("staff:product-list")
def form_valid(self, form):
form.save()
return super(ProductCreateView, self).form_valid(form)
As I stated at the start i'm very new to both Django and AWS S3, so if this is just a schoolboy error, please forgive me but any help would be fantastic.正如我在开始时所说的那样,我对 Django 和 AWS S3 都很陌生,所以如果这只是一个小学生的错误,请原谅我,但任何帮助都会很棒。 And if any additional info is needed i'll be happy to provide.
如果需要任何其他信息,我很乐意提供。 Thanks!
谢谢!
You need to write some lines in your settings.py
file in order to get django-storages
to work.你需要在你的
settings.py
文件中写一些行来让django-storages
工作。
Those lines are:这些行是:
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
and you need to identify those variables in your .bash_profile
file (if you are on mac) so that they are not committed to your version control system for security reasons.并且您需要在
.bash_profile
文件中识别这些变量(如果您在 Mac 上),以便出于安全原因它们不会提交给您的版本控制系统。
You need to follow this syntax:您需要遵循以下语法:
export AWS_ACCESS_KEY_ID="YOUR_ID" **(NO SPACES)**
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
export AWS_STORAGE_BUCKET_NAME="YOUR_BUCKET_NAME"
and then you need to provide Heroku (on which I encountered this error) with this data by using configVars
.然后您需要使用
configVars
向 Heroku (我遇到此错误)提供此数据。
In your Terminal/CommandLine:在您的终端/命令行中:
heroku config:set AWS_ACCESS_KEY_ID="YOUR_ID"
heroku config:set AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
heroku config:set AWS_STORAGE_BUCKET_NAME="YOUR_BUCKET_NAME"
Make sure to use double-quotes!确保使用双引号!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.