I am stuck with a server 500 server error at a form only when it is about to save the pictures (the rest of the forms belonging to other models are saved in the db ok) I didn't have the problem in development, but I think I know why I am not coming on, because there is an extra configuration for media URL, and that is nginx
Could anyone tell me how to do this well? I have in my production server pulled the github app at:
/home/boards/myapp
In the settings.py:
MEDIA_ROOT = BASE_DIR /'media'
MEDIA_URL = "/media/"
In urls.py
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In Nginx (Here is where I must be screwing up because I don't nginx in development and it works well)
location /media/ {
alias /home/boards/media/;
}
Well, this is the model but like I said, it works great in development
class Photos(models.Model):
def user_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.user, filename)
images = ResizedImageField(size=[500, 300], upload_to=user_directory_path, blank=True, null=True)
I am running gunicorn, nginx and supervisor. I reload nginx, still no changes. By the way I have tried for 4 days to see log errors. Nowhere. I turned debug to true. Nothing to be seen. I added logging. Nothing to be seen I have even installed sentry, no issue is shown, but I have got a big whopping 500 server error when I try to save the form,
this is the view snippet (like I said, it works in development, but not in production). I have also looked into firefox console and all the values from the text fields are posted, including the photo, but there is that 500 error always. I have thought that maybe there were no permissions for the user to create their custom directory on the fly in order to store the pictures, so I created manually and added a sudo chmod 775 to that directory but NOTHING of what I do on the production pages is shown. I can even write words on the index.html page and nothing is shown. The VPS server cache is giving me hell because I can see nothing of my changes. I could delete the whole code and the web would still show there.
if form_photos.is_valid():
print("Form photos is valid")
username = request.user
images = request.FILES.getlist('images')
print("Las imagenes son", images)
for image in images:
Photos.objects.create(images=image, builtproperties_id=last_built_id, user=username)
#messages.success(request, "Data saved")
return redirect('listbuiltaddressphotos')
else: # SI HAY FALLOS EN FOTOS
print(form_photos.errors)
return redirect('create_apartment_form')
With regards to nginx and gunicorn logs Of course I have looked into that and there are no errors logged. When I go to ngix-access.logs it just shows me that there is a 500 server error, which is the same information I get from going through the web browser. I have looked into the console of Firefox, all the values from all fields are POSTED, also the photo, but the return result is a 500 error.
The code on production was a copycat of what was on my development as all I did was push from local pull from server. Then I looked into the views.py code and the line in question needed to create on the fly a directory to save the user's pictures. So that had to be the problem: a permissions issue.
So, I did
sudo chmod 775 directoryand that should have granted the permissions. Somehow it hasn't but I will look into that, the important thing is that I know that is the problem.Also, I managed to run sentry and sentry confirmed exactly what I thought: a permissions issue. Whenever you get a 500, think about a permission problem.