Почему мой код django для метода POST не работает?
Я работаю над сайтом на django и пытаюсь получить данные от пользователя с помощью метода POST. Сообщение об ошибке следующее :
MultiValueDictKeyError at /prediction/
'date1'
Request Method: GET
Почему метод запроса все еще GET, хотя в моем views.py указан POST?
Views.py :
from django.shortcuts import render
from apple.models import predictions
# Create your views here.
def home_view(request):
pre = predictions.objects.filter(date='2021-01-15')
return render(request, 'homepage.html', {'prediction': pre})
def read(request):
final_date = str(request.POST["date1"])
price = predictions.objects.filter(date=final_date)
return render(request, 'results.html', {'price':price})
Это моя 2-я HTML страница Results.html :
<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Hind:300' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<title>Prediction Page</title>
</head>
<body>
<h1>"The Predicted price for this date is:" +
"{{price}}"</h1>
</body>
</html>
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
html {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
width: 100%;
height: 100%;
font-family: 'Open Sans', sans-serif;
background: #331952;
color: #fff;
font-size: 18px;
text-align: center;
letter-spacing: 1.2px;
}
</style>
Это главная HTML-страница - homepage.html (откуда берется 'date1') :
пытались ли вы поместить метод view в представление класса?
from django.shortcuts import render
from django.views.generic import View
class LoginView(View):
def post(self, request):
final_date = str(request.POST["date1"])
price = predictions.objects.filter(date=final_date)
return render(request, 'results.html', {'price':price})
и использовать в urlpatterns
from .views import LoginView
urlpatterns += [
path("/prediction/", LoginView.as_view())
]