0

This is my javascript function where I pass parameters, using alert I checked that function is getting the parameters I want but I am unable to pass them in django url other wise giving a string the url works but not with parameters.

      function myFunction(a) {
      var v = a.value;
      alert(v);
      location.href="{% url 'new_event' v %}"; //does not works
      location.href="{% url 'new_event' 'string' %}"; //works
      }

I have checked the value getting is string the way I want but how to pass it?

2
  • This is likely not due to the parameter in the URL, but because the URL contains escaped characters, or quotes, etc. Commented Oct 8, 2018 at 22:17
  • I am actually passing date and time in url for the sake of simplicity only used string in this post, the actual url is {% url 'new_event' '08:30:00' '2018-10-08' %} Commented Oct 8, 2018 at 22:22

2 Answers 2

1

Use

location.href = "/" + v

where v is your id or slug

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

Comments

0

kindly try this:

    function myFunction(a) {
        var v = a.value;
        alert(v);
        location.href="{% url 'new_event' v %}"; // this will not work because your django **url** filter is pre-processed on your server while your javascript variable is processed on **client**
        // if you want your variable v to be dynamic, you need to include it in your django's view **context**
        location.href="{% url 'new_event' 'string' %}"; //works
    }

    // include in your views.py
    context['v'] = 'some-string'

    // template.html
    <script>
        function myFunction(a) {
            var v = '{{ v }}'; // javascript variable v
            alert(v);
            location.href="{% url 'new_event' v %}"; // django context variable v
        }
    </script>

1 Comment

Reverse for 'new_event' with no arguments not found

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.