0

I need to use data submitted by user in one view in another view. I don't want that data in the URL, and the data can not be extracted by calling the last object created, since many times it will not be that object.

My view for the index looks like this:

def index(request):

    template_name = 'index.html'

    if request.method  == 'POST':
        foo = request.POST['foo_text']
        Foo.objects.get_or_create(foo=foo)
        return redirect('/results/')

    return render(request, template_name, {})

The template for this view is:

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

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

The view to handle the data results is:

def results_view(request, foo):

    template_name = 'movement_results.html'
    results = Results.objects.filter(foo=foo)

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

So as you can see the User will type some data, foo, into the index.html, which then gets or creates the object if it does not already exist in the database. From that the results view will return results based on the data submitted by the user previously. How do I get the foo data to the next view?

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
user2770624
  • 293
  • 4
  • 18
  • Please follow the [django tutorial](https://docs.djangoproject.com/en/1.8/intro/), you will save a lot of people a lot of time. – spectras Sep 24 '15 at 19:24
  • Your code doesn't make much sense to me. Your form submit would `POST` the data to `/results/`, which points to `index` method, then you `redirect` to `/result/` again? Sounds like `render_view` is never visited? – Shang Wang Sep 24 '15 at 19:26
  • Would I be better off submitting the data to the index then redirecting to results view? – user2770624 Sep 24 '15 at 19:29
  • @spetras I already went through the tutorial, and a bunch of others. If there was something I missed that would have answered my questioned I feel that it would be a lot more beneficial to direct me towards that page instead of berating for asking a question. – user2770624 Sep 24 '15 at 19:33

1 Answers1

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})
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • Thank you, I was trying to get away from putting the foo.id like results/foo.id in the url that is why I asked the question. I really just wanted results/ to be able to retrieve the id without it being in the url. Would sessions and cookies be what I should be focusing on, or am I just giving myself a bunch of unnecessary work? – user2770624 Sep 24 '15 at 20:05
  • 1
    There are so many ways to do it, you could use django session variables, use temp files, message queues like rabbitmq, or in memory database like redis, etc. Maybe start with django session. – Shang Wang Sep 24 '15 at 20:10
  • Thank-you again, this is exactly what I was looking for. – user2770624 Sep 24 '15 at 20:13
  • 1
    If you are not sure, google is your friend. By googling django session gives me this: http://stackoverflow.com/questions/14465993/how-can-i-set-and-get-session-variable-in-django – Shang Wang Sep 24 '15 at 20:14