Reputation: 18735
I can't figure out how to render an editable table using Django-datatable-view
. I want to create a table exactly like this: http://django-datatable-view.appspot.com/x-editable-columns/ from model City
for example.
I've read the docs and tutorials but still can't figure out how to create the table.
This is what I've done so far:
{% extends "base.html" %}
{% block head %}
{% load static %}e
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<script src="{% static "datatable/js/datatableview.min.js" %}"></script>
<script>
datatableview.auto_initialize = false;
$(function () {
var xeditable_options = {};
datatableview.initialize($('.datatable'), {
fnRowCallback: datatableview.make_xeditable(xeditable_options),
});
})
</script>
{% endblock %}
{% block content %}
{{ datatable }}
{{ object_list }}
{% endblock %}
And my view is:
class ZeroConfigurationDatatableView(dtv_views.XEditableDatatableView):
model = dolava_models.City
datatable_options = {
'columns': [
'id',
("Name", 'name', dtv_helpers.make_xeditable),
("Country", 'country', dtv_helpers.make_xeditable),
]
}
Unfortunately my code renders this:
Upvotes: 1
Views: 9816
Reputation: 11
if think you will need this function to render custom column as per your need
def render_column(self, row, column):
# We want to render user as a custom column
if column == 'user':
# escape HTML for security reasons
return escape('{0} {1}'.format(row.customer_firstname,
row.customer_lastname))
else:
return super(OrderListJson, self).render_column(row, column)
Upvotes: 1
Reputation: 8144
As the first step, try to run the example project that is buried in:
django-datatable-view/datatableview/tests/example_project/example_project/example_app
To do this, follow the Documentation and Live Demos on the projects github page, but be sure to replace django>=1.4
with django==1.8
before you start. That is, it is not compatible with django1.9 or 1.10 in my experience.
Once you accomplish this, you will have a full set of working examples and the matching documentation which will ease things a bit. The link that you gave is out of date. Explore the examples (example_app/views.py
), and the documentation.
When you are ready to get out of the sandbox, follow this route:
Start a new virtualenv
Clone this branch: https://github.com/jangeador/django-datatable-view/
(it has a few more commits that make it compatible with recent versions of django), and pip install (use the -e editable option if you plan to make changes).
Follow of this example Datatable object and Meta from the example app that you run. In code:
class MyDatatable(Datatable):
class Meta:
model = Entry
columns = ['id', 'headline', 'pub_date', 'n_comments', 'n_pingbacks']
ordering = ['-id']
page_length = 5
search_fields = ['blog__name']
unsortable_columns = ['n_comments']
hidden_columns = ['n_pingbacks']
structure_template = 'datatableview/default_structure.html'
class ConfigureDatatableObjectDatatableView(DatatableView):
model = Entry
datatable_class = MyDatatable
You can delete all attributes in the Datatable
class if you don't need (they are optional).
Apart from the documentation I had to override the following method on the DataTableView
class to make it work.
def get_template_names(self):
return "example_base.html"
This is your template file, which needs to contain:
<script src="{% static 'path-to/datatables.min.js' %}" type="text/javascript"></script>
<link href="{% static 'your-path-to/datatables.min.css' %}" rel="stylesheet">
<script type="text/javascript" src="{% static 'js/datatableview.js' %}"></script>
{{ datatable }}
<script>
// Page javascript
datatableview.auto_initialize = true;
</script>
What is noteworthy here is the inclusion of datatableview.js
from the django-datatable-view module (and the accompanying datatableview.auto_initialize = true;
). If you are comfortable with the datatables.js to begin with, you can go ahead and configure it by yourself, but given you are also new to django-datatable-view also, this is probably the easiest route.
Upvotes: 3
Reputation: 4777
{% block content %}
{% if instances %}
<table class='datatable'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
</tr>
{% for row in instances %}
<tr>
<th> {{ row.id }}</th>
<th> {{ row.name }}</th>
<th> {{ row.country }}</th>
</tr>
{% endfor %}
</table>
{% else %}
<p>No data available</p>
{% endif %}
{% endblock %}
Upvotes: 0