2

有没有办法可以将表单提交中的数据传递到“谢谢”页面。我想这样做的原因是因为我在网站上有一个表单,用户将在其中选择多个字段,这些字段都包含不同的 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

表格.py

class ReportEnquiryForm(forms.ModelForm):

    class Meta:
        model = models.DocumentRequest

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

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

4

2 回答 2

0

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

编辑这个想法是使thank_you页面像接受来自URL的参数的任何其他视图一样:

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'])
于 2013-10-03T12:36:49.660 回答
0

你可以像 Daniel Roseman 所说的那样做,但在这种情况下,任何拥有 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 查询数据库。

于 2013-10-03T13:00:49.103 回答