Hossam Salah
Hossam Salah

Reputation: 185

Passing Id from CreateView to another function after creating the object

I am creating a Payment object using Django CreateView then I want to pass the id of this object to another view function to make and display some calculations on another template. how can i do that?

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>

urls:

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

Upvotes: 2

Views: 1257

Answers (2)

Hossam Salah
Hossam Salah

Reputation: 185

I did it by overriding the get_success_url in CreateView like this:

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

Upvotes: 1

The_Cthulhu_Kid
The_Cthulhu_Kid

Reputation: 1859

Add a parameter to your other view and url and use get_success_url:

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

Upvotes: 2

Related Questions