开发者

Pass select option value into named url pattern using Django and Jquery

开发者 https://www.devze.com 2023-01-27 05:48 出处:网络
I have a dropdown list in my django template: <select name=\"submit_new\" id=\"submit_new\"> <option value=\"Stdo\">Studio</option>

I have a dropdown list in my django template:

<select name="submit_new" id="submit_new">
    <option value="Stdo">Studio</option>
    <option value="Flm">Film</option>
    <option value="Drctr">Director</option>
</select>
<button type="submit_type" id="submit_new_button">Go</button>

I would like to take the value of the selection and pass it into the django named url pattern

$("#submit_new_button").click(function() {
    var NewLink = {% url add_record $("#submit_new").val() %}
    window.location.href = NewLink
});

Clearly, as written, this does not work. How can I used the named pattern {% url add_record VAR %} in combination with the jquery value $("#submit_new").val()?

edit:

My url entr开发者_开发知识库y for add_record is:

url(r'^add_(?P<modelname>\w+)/$', views.generic_change, name = 'add_record')


Definitely a problem with the add_record entry in urls.py but the template needs a little help as well. Try being explicit with all the arguments for that url entry. You can switch to more of a short hadn notation later but for now just try this to get things working:

url(r'^add_(?P<modelname>\w+)/$', 
    view='your_app.views.generic_change', 
    name='add_record')

And then try this in your template:

$("#submit_new_button").click(function() {
    var link = '{% url add_record 'NEWVALUESTUB' %}';
    link = link.replace('NEWVALUESTUB', $("#submit_new").val());
    window.location.href = link;
});


<script>
$("#next-button").click(function() {
    var countryid = $('input:radio[name=countries_list]:checked').val();
    var link = "{% url 'selectbank' value=2 %}";
    link = link.replace('2', countryid);
    window.location.href=link;
    });
</script>

I was having same issue with radio buttons. Followed the above answer. But I got the NoReverseMatch found error.

I could able to solve it by this method.

0

精彩评论

暂无评论...
验证码 换一张
取 消