How to return result from a function in Django html form?
<p>Basically I have this form right here</p>

<pre><code>&lt;form method="POST" action="."&gt;
   &lt;input type="text" name="fcal" value="{{F}}" /&gt;
&lt;p&gt;
   &lt;input type="submit" name="fibo" value="Fibonacci" /&gt;
&lt;/p&gt;
&lt;/form&gt;
</code></pre>

<p>I would like the user to input a number and after that this number to be calculated and printed in a Fibonacci sequence.</p>

<p>This is my Fibonacci function</p>

<pre><code>def fibo(n):
    if n == 0:
       return 0
    elif n == 1:
       return 1
    else:
       result = fibo(n - 1) + fibo(n - 2)
       return result
</code></pre>

<p>I believe my problem is coming from the views.py file because I am not sure how to create the result to be rendered back.</p>

<pre><code>from django.template import Context, loader, RequestContext
from django.shortcuts import render_to_response, get_object_or_404
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_to_response('fibb/fibb.html', context_instance=RequestContext(request, cal))
    else:
       print('Wrond input type')
</code></pre>

<p>I would highly appreciate if someone can at least explain how to get the result from a function and print it out on the same page.</p>

<p>your html file</p>

<pre><code>    &lt;form method="POST" action="."&gt;
       &lt;input type="text" name="fcal" value="{{F}}" /&gt;
    &lt;p&gt;
       &lt;input type="submit" name="fibo" value="Fibonacci" /&gt;
    &lt;/p&gt;
    &lt;/form&gt;

{% if cal %}
      Your Fibonacci answer is {{cal}} 
{% endif %}
</code></pre>

<p>Your view:</p>

<pre><code>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', {})
</code></pre>

<p>This will do the trick</p>

python django