1

Inside an html page I have two radio buttons with values A and B. I also have an url to a view that needs two parameters, one is a parameter that I can easily access through the django context variable but the second parameter should have the value A or B based on which radio button is checked. I don't know how to get this done.

I've red this post: Get javascript variable's value in Django url template tag which is kinda what I'm looking for but I don't understand how can this be implemented in my case.

Also checked this post: https://www.reddit.com/r/django/comments/39q9lm/url_tag_in_template_with_a_dynamic_parameter/ which is pretty much the same concept.

What I tried to do in my template is:

<option value="{% url 'view' tmp i %}".replace(/tmp/, 'A');> </option>

which is not exactly what I'm aiming for but it was just to check if this is working.. This is not working to me though. Of course my goal is not to replace tmp with 'A' but with 'A' or 'B' based on which radio button is checked by the user inside my html page. Radio buttons have ids radio1, radio2.

I could use javascript of course but I'd like to stay away from JQuery or Ajax or other stuff in general..

Any help is appreciated Thank you

looppool
  • 13
  • 2

1 Answers1

0
  `
<option id="mylink" value="{% url 'view' A i %}"> </option>

<id='radio1' onmousedown="document.getElementById('mylink').value = '{% url 'view' tmp i %}'.replace('tmp', 'A')">
<id='radio2' onmousedown="document.getElementById('mylink').value = '{% url 'view' tmp i %}'.replace('tmp', 'B')">

then when u select the radio button it will change the # to the proper link

Jay
  • 1,289
  • 3
  • 11
  • 22
  • ```replace('tmp', 'A');``` is what I want to be "dynamic". What I mean by that is that i should replace with 'A' if radio1 is checked, else i should replace with 'B'. That is the goal but with the command you mentioned i always replace with 'A' no matter what right? – looppool Aug 28 '19 at 21:33
  • Post the full html radio button code. You can do something like "onmousedown" for both radio buttons, and when the radio button is clicked, it changes the link to whichever is selected. – Jay Aug 28 '19 at 21:36
  • Html is toooo messy, multiple pages too. But the elements needed are these: ``` ``` and the idea is: if radio1 is checked replace parameter of url with A, if radio2 is checked replace parameter of url with B... – looppool Aug 28 '19 at 21:43
  • Well that's as far as i can go w/o seeing the link you click to go to "view". (I'm assuming it goes to the url on a submit button/command and not on the radio button selection, yes? In case you want to toggle? I'd have to see the html for the submit command to do more. – Jay Aug 28 '19 at 21:47
  • What is the url html, tho? a submit button? a link? I need to see that. – Jay Aug 28 '19 at 21:48
  • I don't have a submit button, the only link is that on the option element.. it's a select element with several options. Each option has the url you can see on the code of my question. In fact, if you select an option, that option redirects you to the page indicated on the value of the option – looppool Aug 28 '19 at 21:50
  • my edit above should work then. It has A as the default, but you can change it to whatever u want. I can't guess anymore without seeing ur code tho. – Jay Aug 28 '19 at 21:53
  • 1
    Ok I think that might work, I will try asap, if it works I'll mark it as accepted, thank you – looppool Aug 28 '19 at 22:06