I have a Django app with a PostgreSQL database hosted on Railway. I am able to retrieve and display all data from the database on my website except for images.
Here is my relevant code:
# views.py
@csrf_exempt
def addpost(request):
if request.method == "POST":
title = request.POST['title']
content = request.POST['content']
author = request.POST['author']
date = request.POST['date']
categories = request.POST['categories']
upload = request.FILES.get('image')
newpost = NewPost(
title=title,
content=content,
author=author,
publication_time=date,
categories=categories,
image=upload
)
newpost.save()
return render(request, "addpost.html")
# models.py
class NewPost(models.Model):
title = models.CharField()
content = models.TextField()
author = models.ForeignKey(User)
publication_time = models.DateTimeField()
categories = models.TextField()
image = models.ImageField(upload_to="images/")
The image field is saving to the database correctly, but not displaying on the front end. All other data displays fine.
I have confirmed that:
The images are saving to the database
All other data displays correctly
It seems to be an issue displaying the images only.
Any ideas what I'm missing in order to get the images to display from the PostgreSQL database?