Python Forum

Full Version: Django and AWS S3 media storage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have an app that will upload photos and videos but i keep getting server error 500. I am not sure how to configure the AWS S3 for media correctly. Here is what I have:

I have aws folder that has this file conf.py
DEFAULT_FILE_STORAGE = '{appname}.settings.aws.storage_backends.MediaStorage'
#DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Srorage'
AWS_ACCESS_KEY_ID = '???'
AWS_SECRET_ACCESS_KEY = '???'
AWS_STORAGE_BUCKET_NAME = '???'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = 'public-read'
AWS_QUERYSTRING_AUTH = False

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)
storage_backends.py
from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False
I am trying to add a product an when submitted i am getting the error 500

here is the codes for the add product:

product model:
class Product(models.Model):
	name = models.CharField(max_length=500, null=True)
	price = models.FloatField()
	category = models.ForeignKey(Category, on_delete=models.CASCADE, default=True, null=False)
	digital = models.BooleanField(default=True, null=True, blank=False)
	image = models.ImageField(upload_to='images/',null=True, blank=True)
	video_name= models.CharField(max_length=500)
	video_file= models.FileField(upload_to='videos/', null=True, verbose_name="")
	video_demo= models.FileField(upload_to='videos_demo/', null=True, verbose_name="")
	
	def __str__(self):
		return self.name

	@property
	def imageURL(self):
		try:
			url = self.image.url
		except:
			url = ''
		return url
forms.py
class ProductForm(ModelForm):
	class Meta:
		model = Product
		fields = ('name','price','category','digital','image','video_name','video_file', 'video_demo')
		
		labels = {

		'name': '',
		'price':	'',
		'category':	'',
		'digital': 'Digital?',
		'image': 'Upload Image',
		'video_name': '',	
		'video_file': 'Upload Video',
		'video_demo': 'Upload Video Demo',

		}

		widgets = {

		'name': forms.TextInput(attrs={'class':"form-control", "placeholder":"Product Name"}),
		'price':	forms.TextInput(attrs={'class':"form-control", "placeholder":"Product Price"}),
		'category':	forms.Select(attrs={'class':"form-control"}),
		'digital': forms.CheckboxInput(attrs={'class':'form-control'}),
		'image': forms.FileInput(attrs={'class':'form-control'}),
		'video_name': 	forms.TextInput(attrs={'class':"form-control", "placeholder":"Video Title"}),
		'video_file':	forms.FileInput(attrs={'class':'form-control'}),
		'video_demo':	forms.FileInput(attrs={'class':'form-control'}),
		}
views.py
def add_product(request):
	submitted = False

	if request.method=="POST":
		
		form = ProductForm(request.POST or None, request.FILES or None)
		
		if form.is_valid():
			form.save()

		return HttpResponseRedirect('/add_product?submitted=True')
	
	else:
		form = ProductForm
		if 'submitted' in request.GET:
			submitted = True

	return render(request, 'add_product.html', {'form':form, 'submitted':submitted})
and add_product.html
{% extends 'cart/main.html' %}
{% load static %}

{% block content %}

<center>
	<h1>Add New Product</h1>
<center>
	<br/><br/>
	
	{% if submitted %}
	
		Your product was added...
	
	{% else %}

		<form action="" method="POST" enctype="multipart/form-data">
			{% csrf_token %}
			{{ form.as_p }}

			<input type="submit" value="submit" class="btn btn-secondary">

		</form>

	{% endif %}


{% endblock content %}
thanks.