[Fixed]-Using data from one view in another view

1👍

I think you are confused of where the form should be submitted to. You wrote the code to accept POST in index(), so you definitely should POST to index(). Also you defined foo as a parameter for results_view, then when you redirect, you can’t simply redirect to /results/, but /results/<foo_id>.

Although you’ve implemented most of the general flows correctly, there are many details I mentioned above you are missing, so please read more carefully about the django manual.

Here’s my attempt to solve your confusion(untested):

index method:

def index(request):    
    template_name = 'index.html'
    if request.method  == 'POST':
        foo_text = request.POST['foo_text']
        foo, created = Foo.objects.get_or_create(foo=foo_text)
        return redirect('/results/%s' % foo.id)

    return render(request, template_name, {})

index.html:

<html>
<title> Some Site </title>  
<h1> Some site </h1>

<form action="" method="POST" > 
    <input type="text" name="foo_text" id="id_foo_lookup" placeholder="Enter Your Foo">
    {% csrf_token %}
</form>

results_view method:

from django.shortcuts import get_object_or_404

def results_view(request, foo_id):    
    template_name = 'movement_results.html'
    result = get_object_or_404(Result, pk=foo_id)

    return render(request, template_name, {'result' : result})

Leave a comment