I am using datatables with django. I am having trouble passing parameters in render method.

I am using datatables with django. I am having trouble passing parameters in render method.

rocky331rocky331 Posts: 3Questions: 1Answers: 0
edited June 2019 in Free community support

I have a table of five columns in my html. The fifth is href with parameters.
In django, reversematch is used to match the url by name.

Urls.py:
re_path(r'^download/(?P<req>\d+)$', views.download, name='download')

In Javascript:

$('#table_id').dataTable({
                "processing": true,
                "ajax": {
                    "processing": true,
                    "url": "{% url 'ajax_request_data' %}",
                    "dataSrc": ""
                },
                "columns": [
                    {"data": "fields.columnvalue1"},
                    {"data": "fields.columnvalue2"},
                    {"data": "fields.columnvalue3"},
                    { "data": "fields.columnvalue4"},
                    {
                        "data": "fields.columnvalue5",
                        "render": function (data, type, row, meta) {
                            if (type === 'display') {
                                data = '<a href="{% url 'download' row.pk %}">Download</a>';
                            }
                            return data;
                        }
                    }
                ]
            });

The above gives the following exception:
NoReverseMatch at /requests/
Reverse for 'download' with arguments '('',)' not found.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @rocky331 ,

    I'm not familiar with Django, but it looks like it's got something to do with your urls.py string. Does it need a forward-slash before the '$'?

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,268Questions: 26Answers: 4,765
    Answer ✓

    Most people on this forum aren't very familiar with Django nor Python. You will need to provide more details.

    The above gives the following exception:
    NoReverseMatch at /requests/
    Reverse for 'download' with arguments '('',)' not found.

    Where do you see this exception?

    When does it occur?

    Have you used the browser's developer tools to see the request/response traffic when the error occurs?

    I'm guessing the problem is occurring when clicking the Download URL in the table. What does the URL look like?

    Likely the problem is not a Datables specific issue. If you are having problems rendering the proper URL and need help please post a simple test case representing your data so we can take a look.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • rocky331rocky331 Posts: 3Questions: 1Answers: 0

    The exception occurs in the render method of javascript.

    (Figure 2)In html, if I write:
    <a href="{% url 'download' req.pk %}">
    <button class="btn btn-outline-warning mr-3">{{ req.pk }} Download TSDF</button>
    </a>

    This works perfect.

    How do I send the pk(primary key) parameter in the render method?
    The figure 1 shows the table generated using datatables. Have a look at where the pk value is. I want it to be like the the href attribute in the figure 2.

    Ignore the other stuff in the href. I made up an url just to display. The figure 2 works perfectly with all the functionality of downloading.


  • kthorngrenkthorngren Posts: 20,268Questions: 26Answers: 4,765
    Answer ✓

    How do I send the pk(primary key) parameter in the render method?

    Is pk part of the row data?

    If it is then you can access it using the row parameter. The columns.render docs have some examples of rendering HTML. This example shows how to access the row data. Without seeing an example of your data its hard to say exactly what to use but the example has row[3] but you are using objects so you would use row.fields.pk maybe?

    Kevin

  • rocky331rocky331 Posts: 3Questions: 1Answers: 0
    edited June 2019

    Hey @kthorngren, thank you for the help.

    I saw the docs and was able to resolve it:
    My render function is:

                        var url = "{% url 'download' 0 %}";
                        var id = row.pk;
                        url = url.replace(0, id);
                        data = '<a href=' + url + '>Download TSDF</a>';
    
This discussion has been closed.