2013-10-03 20 views
1

有沒有一種方法可以將表單提交中的數據傳遞給「謝謝」頁面。我想這樣做的原因是因爲我在網站上有一個表單,用戶將在其中選擇多個包含不同PDF的字段。從我的視圖中傳遞表單數據以表示感謝頁面

所以一旦用戶提交了表單,這個想法就是將他們重定向到一個thankyou頁面,他們可以在這裏查看他們在表單上選擇的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') 

如果您不再需要的信息,請諮詢:)

回答

1

你已經保存在用戶提交的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})) 

和thank_you是:

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']) 
+0

嗨丹感謝您的回覆。我現在聽起來像是一個真正的n00b。有沒有辦法給我一個例子?這對我來說都是新的,我試圖理解這一切。 – JDavies

+0

注意:任何人都可以訪問謝謝頁面。如果您不希望用戶返回到感謝頁面,則可以在用戶訪問該頁面後使用會話並刪除會話變量。那麼你將不需要做這些改變。 – moenad

+0

感謝您的回覆,我收到此錯誤。 '查看contact_enquiries.views.document_request沒有返回HttpResponse對象。'這是因爲我需要返回一個空表單嗎?如果是這樣,我該如何去做呢? – JDavies

0

你可以做丹尼爾·羅斯曼說,但在這種情況下,感謝您的頁面可以被任何人訪問與Ids。

一些通過視圖之間數據的方式有以下幾種(名單不是我的。):

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查詢數據庫。

相關問題