Формы POST запрос
Содержимое файла urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^my_page_with_form/', views.my_page_with_form, name='my_page_with_form'),
url(r'^my_page_with_answer/', views.my_page_with_answer, name='my_page_with_answer'),
url(r'^count_summa_of_numbers/', views.count_summa_of_numbers, name='count_summa_of_numbers')
]
Содержание файла views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
def my_page_with_form(request):
return render(request, 'prilogenie111/my_page_with_form.html', {})
def my_page_with_answer(request):
return render(request, 'prilogenie111/my_page_with_answer.html', {})
def count_summa_of_numbers(request):
a = str( request.POST['a'] )
b = str( request.POST['b'] )
summa = float(a) + float(b)
print("Find Summa:")
print(a)
print(b)
print(summa)
return HttpResponseRedirect("/my_page_with_answer/" + str(summa))
Содержание HTML страницы с формой ввода чисел
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Page</title>
</head>
<body>
<h1>Form Page</h1>
<hr>
<form action = "/count_summa_of_numbers/" method = "POST">
{% csrf_token %}
A <input type = "text" name = 'a'>
<br>
<br>
B <input type = "text" name = 'b'>
<br>
<br>
<input type = "submit" value = "Получить сумму">
</form>
</body>
</html>
Содержание HTML страницы, на которой выводится результат вычислений
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Answer Page</title>
</head>
<body>
<h1>Answer Page</h1>
<hr>
<h3 id = "myContent"></h3>
<a href = "/my_page_with_form/">Вернуться</a>
<script>
"use strict";
window.onload = function() {
const myContent = document.getElementById("myContent");
const mass = (window.location + "").split("/");
const answer = mass[mass.length - 1].toString();
myContent.innerHTML = answer;
}
</script>
</body>
</html>
Last updated
Was this helpful?