2016-07-20 56 views
0

我有一個CBV調用一些模型的方法(其中的計算機上運行某些進程,並連接到其他網站檢索數據)Django如何獲取與發送的帖子中的數據get?

class Edit(View: 
    def get(self, request): 
     object = Model.objects.get() 
     object.foo() 
     return object 

    def post(self, request): 
     ...how can I get the object here without looking it 
     up and calling the method again 

我想在後方法再次獲得對象,但我不想再次調用它,因爲我不想再次運行該過程。有什麼方法可以獲得這些信息嗎?它是通過context

+0

這取決於你在返回的對象是什麼類型的數據。如果數據是用戶特定的,那麼你可以使用會話https://docs.djangoproject.com/en/1.10/topics/http/sessions/,如果不是那麼你可以使用Django的緩存https://docs.djangoproject。 com/en/1.10/topics/cache/ – mastazi

+0

你的意思是「我不想再次運行這個程序」?這兩種方法用於處理不同類型的請求 - 「get」只針對「GET」請求運行,「post」針對「POST」請求運行。 – solarissmoke

回答

1

傳遞到模板這將是請求(reference)的屬性。

data = request.POST # python dictionary-like 

的觀點得到順序的說法:要求,位置的URL參數列表,名爲url arugments作爲字典(doc reference)

def post(self, request, *args, **kwargs): 
    post_data = request.post 
    get_data = request.GET 
    non_named_url_argument_list = args 
    named_url_argument_dict = kwargs 
+0

我仍然看不到我如何檢索通過get請求發送的上下文。我看到我的表單被傳遞到了post請求中,但是如果我打印出request.GET.items(),那裏看到的內容很少,絕對沒有任何我傳遞給上下文的信息 – deltaskelta

+0

我更新了回答,這可能會有所幫助。 – DurgaDatta

+0

ahh是的,我使用了錯誤的形式,並沒有發送正確的數據,因此讓我很困惑 – deltaskelta