I am trying to display an image from an imagefield in Django. The imagefield works correctly. I can save an image to local storage, but the problem is that when the server is running, I see a url in the place of the imagefield. I'm guessing it should be a url to the image, but when I click on it, it just reloads the current page. How can I make it so that when I click on it, I am taken to a page with the image? This is how I have tried doing it, but it doesn't work:
#views.py
class CreateView(generics.ListCreateAPIView):
"""This class defines the create behaviour of our REST Api"""
queryset = BucketList.objects.all()
serializer_class = BucketListSerializerPostOnly
def perform_create(self, serializer):
"""Save post data when creating a new bucketlist"""
serializer.save()
class DetailsView(generics.RetrieveUpdateDestroyAPIView):
"""This Class handles the http GET, PUT, and DELETE requests"""
queryset = BucketList.objects.all()
serializer_class = BucketListSerializer
# shows image in a new tab?
def show_image(request, img):
context = {
'image': img
}
return render(request, 'images.html', context)
# my html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="/media/documents/{{ image }}">
<a href="/">Go Back</a>
</body>
</html>
# urls.py
urlpatterns = [
url(r'^bucketlist/$', CreateView.as_view(), name='create'),
url(r'^', CreateView.as_view(), name='create'),
url(r'^bucketlist/(?P<pk>[0-9]+)/$', DetailsView.as_view(), name='details'),
url(r'^media/documents/(?P<img>[\w\-]+)/$', views.show_image,
name='view_image')
]
urlpatterns = format_suffix_patterns(urlpatterns)