2011-06-30 61 views
-1

我有一個索引視圖驗證包含各種數據的表單。儘管thankyou.html頁面沒有複雜的計算來終止服務器,但是如果thankyou.html已經低級緩存,我想渲染一個不同的html頁面。說實話,我不知道通過它的關鍵是什麼...這是代碼。Django低級緩存視圖

DEF指數(請求):

               形式=窗體()

               消息='不正確的數據!

               如果request.method == 'POST':

                               形式=窗體(request.POST)

                               如果form.is_valid():

                                               嘗試:

                                                                User.objects.get(代碼=形式。cleaned_data [ '代碼'])

                                               除了(KeyError異常,ObjectDoesNotExist):

                                                                U =用戶(姓= form.cleaned_data [ '姓氏'], 姓=形式.cleaned_data ['surname'], address = form.cleaned _data [ '地址'], 電子郵件= form.cleaned_data [ '電子郵件'], 電話= form.cleaned_data [ '電話'], 代碼= form.cleaned_data [ '代碼'], )

                                                                u。保存()

                                                               回報HttpResponseRedirect( '/感謝/')

                                                                #return重定向( '三江源')

               選擇render_to_response回報( '的index.html',{ '消息':消息時, '形式':形式},                                                                context_instance = RequestContext的(請求))。

我想這是我應該低一級緩存的方式:如果form.is_valid

                               ():

                                               鍵= ???

                                                cached_html = cache.get(鍵)

                                               嘗試:

                                                                  User.objects.get(代碼= form.cleaned_data [ '代碼'])

                                               除了(KeyError異常,ObjectDoesNotExist):

                                                                U =用戶(姓= form.cleaned_data [ '姓氏'], 姓= form.cleaned_data [ '姓'], 地址= form.cleaned_data [ 'address'], email = form.cleaned_data ['email'], phone = form.cleaned_data ['phone'], code = form。cleaned_data [ '代碼'], )
                                                                u.save()

                                                               如果不是cached_html:

                                                                            cached_html選擇render_to_response =( 'ty.html',{}, context_instance = RequestContext的(請求))

                                                                           緩存。集(標號,cached_html,time_until_midnight())

                                                               返回HttpResponseRedirect( '/感謝/')

                                                                #return重定向( '三江源')

回答

2

我認爲在這種情況下,你應該使用字符串( '三江源' + form.cleaned_data [ '碼'])爲重點