Hello.

Wrote the simplest application for calculating body mass index. Everything works fine until a certain point, and then an error begins to appear

"ValueError at / The view indexmassi.views.indexmassi didn't return an HttpResponse object. It returned None instead."

It is solved only by replacing lines with POST lines with forcibly indicated values ​​of weight and height. Then the page is displayed norms and then return back.

The pattern can not reveal. Usually every 4-6 hours such garbage, only it worked, I press F5 and that's it. Today I reinstalled the system, launched the project and immediately got out.

views.py is:

from django.shortcuts import render from .forms import Forma def indexmassi(request): if request.method == "POST": forma = Forma(request.POST) ves = float(request.POST.get("ves")) rost = float(request.POST.get("rost")) im = round(float(ves/((rost * rost) / 10000)), 1) if 1 <= im < 16: opisanie = "Выраженный дефицит массы" elif 16 < im <= 18.5: opisanie = "Недостаточная масса тела" elif 18.5 < im <= 25: opisanie = "Вы нормальный" elif 25 < im <= 30: opisanie = "Избыточная масса тела (предожирение)" elif 30 < im <= 35: opisanie = "Ожирение 1-ой степени!" elif 35 < im <= 40: opisanie = "Ожирение 2-ой степени!" elif im > 40: opisanie = "Ожирение 3-ой степени!" else: opisanie = "Некорректный результат!" return render(request, 'indexmassi/index.html', {"forma": forma, "message": format(im), "opisanie": opisanie}) 

    1 answer 1

    your function will not return an HttpResponse object if the method is not POST (request.method! = 'POST'), add at the end just a redirect to the index page:

     def indexmassi(request): if request.method == "POST": forma = Forma(request.POST) ves = float(request.POST.get("ves")) rost = float(request.POST.get("rost")) im = round(float(ves/((rost * rost) / 10000)), 1) if 1 <= im < 16: opisanie = "Выраженный дефицит массы" elif 16 < im <= 18.5: opisanie = "Недостаточная масса тела" elif 18.5 < im <= 25: opisanie = "Вы нормальный" elif 25 < im <= 30: opisanie = "Избыточная масса тела (предожирение)" elif 30 < im <= 35: opisanie = "Ожирение 1-ой степени!" elif 35 < im <= 40: opisanie = "Ожирение 2-ой степени!" elif im > 40: opisanie = "Ожирение 3-ой степени!" else: opisanie = "Некорректный результат!" return render(request, 'indexmassi/index.html', {"forma": forma, "message": format(im), "opisanie": opisanie}) return redirect('indexmassi/index.html') 
    • It was necessary to connect the redirect library. Not robs so. He is trying to get to the address 127.0.0.1:8000/indexmassi/index.html and writes Page not found (404) The page itself in my URLs is so attached to the path ('', include ('indexmassi.urls')) I have solved the problem so far like this: else: forma = Forma (request.POST) im = 0 opisanie = "Incorrect result!" return render (request, 'indexmassi / index.html', {"forma": forma, "message": format (im), "opisanie": opisanie}) - Heat molecule