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.
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
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
Most people on this forum aren't very familiar with Django nor Python. You will need to provide more details.
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
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.
Is
pk
part of the row data?If it is then you can access it using the
row
parameter. Thecolumns.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 hasrow[3]
but you are using objects so you would userow.fields.pk
maybe?Kevin
Hey @kthorngren, thank you for the help.
I saw the docs and was able to resolve it:
My render function is: