Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call django urls inside javascript on click event

I got a javascript onclick event inside a template, and I want to call one of my Django urls with an id parameter from it, like this :

$(document).on('click', '.alink', function () {
        var id = $(this).attr('id');
        document.location.href ="{% url 'myapp:productdetailorder' id %}"
});

Course this is not working at all. Any idea ?

Thanks in advance !!

like image 429
jsanchezs Avatar asked May 18 '16 22:05

jsanchezs


People also ask

How do I reference a URL in Django?

The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.

Can I use Django tags in Javascript?

yes ,ofcourse you can use django tags in javascript . the JS code written inside django tags is executed on server side and the rest of the JS is executed on client side. there is no need of rendering a JS template . the normal HTML template works.

Is URL deprecated in Django?

will the projects that use it have to change their source code? Yes, if they upgrade to django-4.0, url will no longer be available. Typically if something is marked deprecated, it is removed two versions later, so in django-4.0, since after django-3.2, django-4.0 will be released.

What is URLconf in Django?

A URLconf is similar to a table of contents for our Django-powered web site. It's a mapping between URL patterns and the view functions that need to be called for those URLs. First, we will have to import the view function that we want our server to run when the URL is matched.


2 Answers

You are trying to access javascript variable that is created at user click on frontend within your Django template at the backend. But, you already know that it would not work.

A better option would be to reconstruct the url in javascript:

$(document).on('click', '.alink', function () {
    // Generate URL without "id" bit
    var url = "{% url 'myapp:productdetail' %}";

    var id = $(this).attr('id');

    // Construct the full URL with "id"
    document.location.href = url + "/" + id;
});

If you don't have a django url helper that would return a URL that you need, you can print out just any and simply replace it in javascript like so:

$(document).on('click', '.alink', function () {
    var url = "{% url 'myapp:productdetail' 123 %}";
    var id = $(this).attr('id');

    // Construct the full URL with "id"
    document.location.href = url.replace('123', id);
});
like image 96
Uzbekjon Avatar answered Sep 28 '22 11:09

Uzbekjon


I think the best way to do this is to create a html data-* attribute with the URL rendered in a template and then use javascript to retrieve that.

This way you avoid mixing js/django template stuff together. Also, I keep all of my JS in a separate file outside the view (which is a much better practice in general), and therefore trying to mix these two won't work.

For instance, if you have a url you want, just create an html hidden element:

<input type="hidden" id="Url" data-url="{% url 'myapp:productdetail' id %}" />

Then, in your JS:

$(document).on('click', '.alink', function () {
    var url = $("#Url").attr("data-url");
});      

I frequently use this pattern for dropdown lists so that I don't have to serve all of the options when I first render the page (and this usually speeds it up).

like image 28
Ben Avatar answered Sep 28 '22 11:09

Ben