在另一个视图中使用一个视图中的数据

时间:2022-01-07 13:09:33

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.

我需要在另一个视图中的一个视图中使用用户提交的数据。我不希望URL中的数据,并且无法通过调用最后创建的对象来提取数据,因为很多时候它不会是该对象。

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?

因此,您可以看到用户将在index.html中键入一些数据foo,如果数据库中尚不存在该对象,则会获取或创建该对象。由此,结果视图将根据用户先前提交的数据返回结果。如何将foo数据导入下一个视图?

1 个解决方案

#1


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>.

我认为您对应该提交表单的位置感到困惑。你编写了代码来接受index()中的POST,所以你肯定应该POST到index()。您还将foo定义为results_view的参数,然后在重定向时,您不能简单地重定向到/ results /,而是/ results /

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.

虽然你已经正确实现了大部分的一般流程,但是我上面提到的很多细节你都缺失了,所以请仔细阅读关于django手册的内容。

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:

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:

results_view方法:

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})

#1


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>.

我认为您对应该提交表单的位置感到困惑。你编写了代码来接受index()中的POST,所以你肯定应该POST到index()。您还将foo定义为results_view的参数,然后在重定向时,您不能简单地重定向到/ results /,而是/ results /

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.

虽然你已经正确实现了大部分的一般流程,但是我上面提到的很多细节你都缺失了,所以请仔细阅读关于django手册的内容。

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:

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:

results_view方法:

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})

相关文章