Davtho1983
Davtho1983

Reputation: 3954

Redirect for UpdateView

I have the following view in Django:

class LoanEditView(UpdateView):
    model = Loans
    form_class = LoanForm

    def get_success_url(self):
        return reverse('edit', kwargs={
            'pk': self.object.pk,
        })

I have a url path that works to call the update view:

path('edit/<int:pk>', LoanEditView.as_view(), name='edit')

But now my home page view is broken - I can't seem to redirect from the edit view?

On top of this, I would really like to pass the pk from a link in my html along the lines of:

<a href="{% url 'edit' %}" class="btn btn-primary" pk=loan.pk>Edit</a>

With profound apologies to anyone following my questions today - I've been at this for ten hours and starting to make silly mistakes!

Upvotes: 1

Views: 54

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477045

You specified pk as an argument of the <a ... pk="loan.pk"> tag, not as a named parameter of the {% url ... %} template tag. Using pk=loan.pk does not make any sense anyway, since it is not a template variable (between {{ ... }}), or in a template tag (between {% ... %}), hence that would mean that your HTML literally contains pk="loan.pk", so not the primary key of the loan, but just a string "loan.pk".

You need to specify the primary key pk in the `{% url ... %} template tag, like:

<a href="{% url 'edit' pk=loan.pk %}" class="btn btn-primary" >Edit</a>

Upvotes: 1

Related Questions