将表单数据从我的视图传递到感谢页面

时间:2013-10-03 12:27:00

标签: python django forms

有没有办法可以将表单提交中的数据传递给“谢谢你”'页。我之所以这样做是因为我在网站上有一个表单,用户将选择多个包含不同PDF的字段。

因此,一旦用户提交了表单,我们的想法是将它们重定向到一个谢谢页面,在那里他们可以查看他们在表单上选择的pdf /文件列表。

我希望这是足够的信息继续下去。这是我的观点/模型。

def document_request(request, *args):

   # template = kwargs['name'] + ".html"

    if request.method == 'POST':
        form = forms.ReportEnquiryForm(request.POST)
        print(request.POST)

        if form.is_valid():
                docrequest = form.save()
                return HttpResponseRedirect(reverse('thank_you', kwargs={'id': docrequest.id}))

    else:
        form = forms.ReportEnquiryForm()
        return render_to_response('test.html',{'form':form})

def thank_you(request):


    docrequest = DocumentRequest.objects.get(pk=id)
    return render_to_response('thankyou.html',
                          {'docrequest' : docrequest },                            
                          context_instance=RequestContext(request))

我最初的想法是将数据传递给名为thank_you的新视图。但这不可能。

class DocumentUpload(models.Model):
    name = models.CharField(max_length="200")

    document_upload = models.FileField(upload_to="uploads/documents")


    def __unicode__(self):
        return "%s" % self.name

class DocumentRequest(models.Model):
    name = models.CharField(max_length="200")

    company = models.CharField(max_length="200")

    job_title = models.CharField(max_length="200")

    email = models.EmailField(max_length="200")

    report = models.ManyToManyField(DocumentUpload)

    def __unicode__(self):
        return "%s" % self.name

form.py

class ReportEnquiryForm(forms.ModelForm):

    class Meta:
        model = models.DocumentRequest

        fields = ('name', 'company', 'job_title', 'email', 'report')

如果您需要更多信息,请询问:)

2 个答案:

答案 0 :(得分:0)

您已将用户的提交内容保存在DocumentRequest对象中。因此,您可以在重定向时在URL中传递该对象的ID,并在thank_you视图中,您可以获取DocumentRequest并呈现列表。

修改我们的想法是将thank_you页面设置为接受来自网址的参数的任何其他视图:

url(r'thanks/(?P<id>\d+)/$, 'thank_you', name='thank_you')

因此表单视图的POST部分变为:

if form.is_valid():
    docrequest = form.save()
    return HttpResponseRedirect(reverse('thank_you', kwargs={'id': docrequest.id}))

并且谢谢你:

def thank_you(request, id):

    docrequest = DocumentRequest.objects.get(pk=id)
    return render_to_response('thankyou.html',
                              {'docrequest' : docrequest },                            
                              context_instance=RequestContext(request))

第二次修改

正如其他人所说,这使任何人都可以看到请求。因此,更好的解决方案是将其放入会话中:

    docrequest = form.save()
    request.session['docrequest_id'] = docrequest.id

并在thank_you:

def thank_you(request):
    if not 'docrequest_id' in request.session:
        return HttpResponseForbidden    
    docrequest = DocumentRequest.objects.get(request.session['docrequest_id'])

答案 1 :(得分:0)

你可以像丹尼尔罗斯曼所说的那样做,但在这种情况下,感谢页面可以被任何拥有ID的人访问。

在视图之间传递数据的一些方法如下(列表不是我的。):

GET request - First request hits view1->send data to browser -> browser redirects to view2
POST request - (as you suggested) Same flow as above but is suitable when more data is involved
Using django session variables - This is the simplest to implement
Using client-side cookies - Can be used but there is limitations of how much data can be stored.
Maybe using some shared memory at web server level- Tricky but can be done.
Write data into a file & then the next view can read from that file.
If you can have a stand-alone server, then that server can REST API's to invoke views.
Again if a stand-alone server is possible maybe even message queues would work.
Maybe a cache like memcached can act as mediator. But then if one is going this route, its better to use Django sessions as it hides a whole lot of implementation details.
Lastly, as an extension to point 6, instead of files store data in some persistent storage mechanism like mysql.

最简单的方法是使用会话。只需将id添加到会话并重定向到感谢视图,您将读取id值并使用该id查询db。