2017-07-06 50 views
1

我使用Django創建支付對象CreateView然後我想將此對象的id傳遞給另一個視圖函數,以在另一個模板上製作和顯示一些計算。我怎樣才能做到這一點?在創建對象後將ID從CreateView傳遞到另一個函數

views.py:

class CreatePayment(CreateView): 
    template_name = "inventory/new_payment.html" 
    success_url = reverse_lazy('inventory:payments_page') 
    model = Payments 
    fields = ('payment_number', 'customer','agent', 'amount') 

HTML:

<body> 

<form action="{% url 'inventory:new_payment'%}" method="post"> 
    {% csrf_token %} 
    {{form}} 

    <button type="submit", value="Add">Add</button> 
</form> 

</body> 

網址:

url(r'newpayment/$', CreatePayment.as_view(), name='new_payment') 

回答

1

添加參數到其他視圖和網址,並使用get_success_url

def get_success_url(self): 
    success_url = reverse_lazy('inventory:payments_page', {'id': self.object.pk}) 
    return success_url 
1

我做到了,通過重寫CreateViewget_success_url這樣的:

def get_success_url(self): 
    return reverse('inventory:transaction', args=(self.object.id,)) 
+0

那麼你爲什麼要取消接受我的答案嗎? –

+0

錯誤 –

+0

我只是想知道,希望它沒有被認爲是粗魯! –

相關問題