0

I have a form in Django and would like to add the correct id at the end of the URL. I am not sure how to add the ID at the end dynamically however. Here's some example code.

{% for dealer_payment_account in dealer.dealerpaymentaccount_set.all %}
<p>Account: {{ dealer_payment_account.last_4_digits }}</p>
  <form id="verifyDealerBankAccountForm" action="/verify-dealer-payment-account/{{ dealer_payment_account.id }}" method="POST">
    <div class="form-row">
      <label>Payment</label>
      <input type="text" name="amount">
    </div>
    <button type="submit">Submit</button>
  </form>
{% endfor %}

Specifically I am wondering how to format this:

action="/verify-dealer-payment-account/{{ dealer_payment_account.id }}"

so that I can add the dealer_payment_accound.id to the end of each form.

Here is the URL:

url(r'^verify-dealer-payment-account/(?P<account_number>[0-9]+)/$', verify_dealer_payment_account),

2 Answers 2

1

Modify your form in the template this way:

<form action="{% url 'verify_dealer_payment_account' dealer_payment_account.id %}">

where: verify_dealer_payment_account is a name of the URL pattern.

Docs: https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#url

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried urlnamespace

<form id="verifyDealerBankAccountForm" action="{% url 'your-verify-dealer-payment-account' dealer_payment_account.id %}" method="POST">

NOTE : please add your respective url name in your-verify-dealer-payment-account

For e.g.

path('verify-dealer-payment-account/<str:payment_account_id>',  views.your_view_name, name='your-verify-dealer-payment-account' )

The url name your-verify-dealer-payment-account is used in template.

3 Comments

I added the URL above.
@Jake Mulhern did it work?
it did work once I added a name to the URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.