Answer the question
In order to leave comments, you need to log in
How can I display an image on a website page?
I have a form that accepts images, they are stored in the media folder, but I don't know how to display these images on the page. It is desirable that it shows exactly the last loaded image.
Thank you in advance!!!
# index.html
{% load static %}
{% block content %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
{% endblock content %}
{% extends 'blog/index.html' %}
{% block content %}
hello
{% endblock content %}
class UploadFile(models.Model):
title = models.CharField(max_length=50)
image = models.ImageField(upload_to='media')
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('photo')
else:
form = UploadFileForm()
return render(request, 'blog/index.html', {'form': form})
urlpatterns = [
path('', upload_file),
path('photo/', post_list, name='photo')
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Answer the question
In order to leave comments, you need to log in
views.py
def post_list(request):
return render(request, 'photo.html', {'last_photo': UploadFile.objects.last()})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question