我正在使用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')
答案 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,))