3

Basically I would like a user to input a number in a field and the fibonacci result to be printed but once the input is inserted into the input field I recieve that it returned None instead of an object

I have this code over here for my html form:

{% block content %}
  <form method="POST" action=".">{% csrf_token %}
     <input type="text" name="fcal" value="{{F}}" />
   <p>
    <input type="submit" name="fibo" value="Fibonacci" />
   </p>
  </form>
  {% if cal %}
     Your Fibonacci answer is {{cal}}
  {% endif %}
{% endblock %}

The content of my views.py is this one:

from django.shortcuts import render_to_response, get_object_or_404, render
from django.forms import ModelForm
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseNotFound
import fibonacci


def fibocal(request):
if request.method == 'POST':
   fb = request.POST['fcal']
   cal = fibonacci.fibo(fb)
   return render(request, 'fibb/fibb.html', {'cal': cal})
else:
   return render(request, 'fibb/fibb.html', {})

And my fibonacci function is this one:

def fibo(n):
    if n == 0:
       return 0
    elif n == 1:
       return 1
    else:
        result = fibo(n - 1) + fibo(n - 2)
        return result

I would like to know where the error is caused as I've looked again and again and I can see that I am returning a render not None

Full traceback include:

Traceback:

File "C:\python35\lib\site-packages\django\core\handlers\base.py" in get_response
158. % (callback.__module__, view_name))

Exception Type: ValueError at /fibb/
Exception Value: The view fibb.views.fibb didn't return an HttpResponse object. It returned None instead.
7
  • Are you sure your code is not erroring out somewhere before the call to render? Could you edit your question and post the full traceback? Commented Sep 17, 2016 at 19:00
  • The error occurs only when I input a value in the input form and press the submit button Commented Sep 17, 2016 at 19:02
  • 1
    can you add your code for the fibb view.. Commented Sep 17, 2016 at 19:08
  • 1
    And fix your indentation; the error is very likely to be there. Commented Sep 17, 2016 at 19:09
  • 1
    the error is at fibb view. But you are showing fibo. Show that view Commented Sep 17, 2016 at 19:10

1 Answer 1

1

In your views.py file, is there any fibb function that return a HTTPResponse? In the error trace, Django cannot find that response in fibb function in fibb.views. You can check the url directed to fibb function. Maybe it should be directed to fibocal function as you mentioned above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you such a silly mistake. I just had another function in there and just forgot to change it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.