Я отправляю запрос POST на мой сервер, который содержит как data
и image's name
и distance
.Затем я извлекаю эти данные и отображаю их в виде изображений.
html
файл в моей папке templates
.Проблема, с которой я сталкиваюсь, заключается в том, что переменные обновляются, как видно из HTML, который я напечатал из запроса POST, но webpage
не обновляется.На самом деле, когда я смотрю HTML-код веб-страницы, сами теги даже не появляются.
Я был бы очень признателен за помощь в решении этой проблемы, поскольку я уже давно борюсь с ней.Я не настолько опытен с Django
.Спасибо!
POST-запрос отправлен на сервер:
import requests
payload = {'distance': '30', 'img': 'image.jpg'}
r = requests.post('http://127.0.0.1:8000/imaging', data=payload)
print(r.status_code)
print(r.text)
Html напечатан из объекта POST-запроса:
200
<!-- -->
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Forest's Fishy Friends</title>
<link rel="stylesheet" type="text/css" href="/static/imaging/index.css">
</head>
<body>
<div class="Top">
<h1>Forest's Fishy Friends</h1>
<h3>Imaging Server</h3>
</div>
<div class="Reading">
<p>The distance from the robot to this object is: </p>
<div class="Cool">
<!-- -->
<h1>30</h1>
<!-- -->
</div>
</div>
<div class="Picture">
<p>Images taken from pi:</p>
<!-- -->
<img src="/static/imaging/Pictures/image.jpg" alt="My image" width="200" height="300">
<!-- -->
</div>
<div class="Bottom"></div>
</body>
</html>
Исходный код HTML на веб-странице (который не включает тег img и тег h1):
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Forest's Fishy Friends</title>
<link rel="stylesheet" type="text/css" href="/static/imaging/index.css">
</head>
<body>
<div class="Top">
<h1>Forest's Fishy Friends</h1>
<h3>Imaging Server</h3>
</div>
<div class="Reading">
<p>The distance from the robot to this object is: </p>
<div class="Cool">
<!--
<b>Awaiting a distance...</b>
-->
</div>
</div>
<div class="Picture">
<p>Images taken from pi:</p>
<!--
<b>Awaiting a picture...</b>
-->
</div>
<div class="Bottom"></div>
</body>
</html>
Код в шаблонах / imaging.html:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Forest's Fishy Friends</title>
<link rel="stylesheet" type="text/css" href="{% static "imaging/index.css" %}">
</head>
<body>
<div class="Top">
<h1>Forest's Fishy Friends</h1>
<h3>Imaging Server</h3>
</div>
<div class="Reading">
<p>The distance from the robot to this object is: </p>
<div class="Cool">
<!-- {% if distance %} -->
<h1>{{ distance }}</h1>
<!-- {% else %}
<b>Awaiting a distance...</b>
{% endif %} -->
</div>
</div>
<div class="Picture">
<p>Images taken from pi:</p>
<!-- {% if img %} -->
<img src="{% static img %}" alt="My image" width="200" height="300">
<!-- {% else %}
<b>Awaiting a picture...</b>
{% endif %} -->
</div>
<div class="Bottom"></div>
</body>
</html>
Просмотр, который извлекает данные и отображает их в шаблонах:
@csrf_exempt
def ImagingView(request):
if request.method == 'POST':
distance = request.POST.get('distance')
image = request.POST.get('img')
path = 'imaging/Pictures/'
img = path + image
context = {'distance': distance, 'img': img}
#print(context['distance'])
#print(context['img'])
return render(request, 'imaging.html', context)
else:
return render(request, 'imaging.html')
urls.py (приложение):
urlpatterns = [
path('imaging', views.ImagingView, name='ImagingView'),
path('', views.IndexView, name='IndexView')
]