tushortz
tushortz

Reputation: 5135

pass form name variable into url in django

I would like to pass the name variable of a selected inputbox in my django template into a url without going through the django views or url.py. Is there an easy way to do this.

<div class="form-group">
    <label class="col-xs-3 control-label">Chapter</label>
    <div class="col-xs-5 selectContainer">
      <input class="form-control" type="text" name="chapter_text" value="1"></input>
    </div>
</div>

I want the name value of the input class to be passed into the name argument of the url in the div button below

<div class="form-group">
    <div class="col-xs-5 col-xs-offset-3">
      <div class="btn btn-default"><a href="{% url 'api:by_book' 'name' %}">Download</a></div>
    </div>
</div>

Upvotes: 0

Views: 316

Answers (1)

JorgeDLuffy
JorgeDLuffy

Reputation: 310

You should do it through javascript. Store your url value as a JS variable:

<script> my_url = "{% url 'api:by_book' 'name' %}"; </script>

Change the anchor to a button with a onClick event that calls a function. In that function you can build the URL with my_url and recovering the name of the input you want with JS. After that, make a redirection:

window.location = "http://www.yoururl.com";

Upvotes: 1

Related Questions