创建对象后,将Id从CreateView传递到另一个函数

时间:2017-07-06 11:47:53

标签: python django django-templates django-views create-view

我正在使用Django CreateView创建一个Payment对象,然后我想将此对象的id传递给另一个视图函数,以便在另一个模板上显示和显示一些计算。我怎么能这样做?

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')

2 个答案:

答案 0 :(得分:2)

将参数添加到其他视图和网址并使用get_success_url

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

答案 1 :(得分:1)

我是通过覆盖get_success_url中的CreateView来实现的:

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