I am currently building a project that will be displaying quite a bit of data in the Django admin I would like to substitute the list view used for datatables using django-datatable-view it says in the list for features that it can be dropped in as a replacement for list view. I might be not reading this correctly but from that I think it means totally replace list view so data tables are used by default which is what I want. I am new to Django and there seems to be no documentation on this add-on they have a few samples but no docs on how to actually use features they claim exist has anyone replaced the list view in Django with datatables using this add-on. I want to try and do it by default for all new models created but also the models like auth that I haven't extended yet.
Well, I have tested this app on my localhost, here is some results(too much for comment, so I will answer here)
First, you need to take a look here: http://django-datatable-view.appspot.com/
It has got some documentation about how to implement django-datatable-view. For example:
http://django-datatable-view.appspot.com/zero-configuration/ has got how to write a view to implement a table based on a model,
http://django-datatable-view.appspot.com/ordering/ has got how to get orders in table,
http://django-datatable-view.appspot.com/javascript-initialization/ has got information about js.
Better if you clone the repo and run it in localhost. There you will be able to experiment with the views and templates(as I tried to do/did).
In here: https://github.com/pivotal-energy-solutions/django-datatable-view/blob/master/datatableview/tests/example_project/example_project/example_app/views.py, you will see how multiple types of view(For no configuration table, specific column table etc) has been coded.
Second, what have I tried so far:
My structure for this project was like this:
-Project
manage.py
-myapp(folder)
views.py
models.py
urls.py
-datatableview*(folder)
-projectapp(folder)
settings.py
urls.py
*From cloned repo, I copied datatableview folder and pasted it in my project.
In myapp>models:
class Post(models.Model):
title= models.CharField(max_length=150)
body = models.TextField()
created = models.DateField()
In myapp>views:
class MyView(DatatableView):
model = Post
datatable_options = {
'columns': [
'title',
'body',
'created',
]
}
In myapp>urls:
url(r'^$', MyView.as_view(), name='myview'),
In templates:
in (tempaltes/myapp/post_list.html)
{% block content %}
{{ datatable }}
{{ object_list }}
{% endblock %}
Result was like this:
title body created
[<post: one >, <post: two>]
here title body created are names of table's column header.
PS: I know its not much of a help, but hopefully this information will help you go further. And a little recommendation, please take a look at django-tables2
Related
I was moving a site over to wagtail and decided to use the codered extensions. The library comes with a image-gallery content-block. I want to use this but define a few templates you can choose from in the admin UI.
You usually define a template in the meta section, but I noticed a dropdown in the admin UI for a template.
How do I add a template to that dropdown? Link to the content block I want to change
I am interested in adding an HTML template and not inheriting from the content-block to change behaviour. (Unless inheriting is the only way to add a template to the dropdown.)
You could paramatise the path to the template you want to use then use an include in your block template to point to the chosen one.
For example, if you had a card block with selection for vertical or horizontal format. In your card block class you might have an property named template that uses a choice block, something like
class AlignmentChoiceBlock(ChoiceBlock):
choices=[
('blocks/flex/vertical_card.html', 'Vertical'),
('blocks/flex/horizontal_card.html', 'Horizontal')
]
Then in your block template, it just consists of:
<div class="some-block-container">
{% include value.template %}
</div>
Well, this works for Wagtail at least, not sure about codered.
The answer from Richard Allen works wagtail and is perfect for your own blocks etc. Wagtail blocks define a separate field that is used for their included components, for this you need another approach.
First you need to add the CRX_FRONTEND_TEMPLATE_BLOCKS to your django settings mysite/settings/base.py.
Then create a folder for your block templates in mysite/website/templates and create a custom template. Then add this path as a entry to the CRX_FRONTEND_TEMPLATE_BLOCKS. Entry key should be the block in lowercase. For a starter you could copy a template/html file from the codered package, found in coderedcms/blocks/
Now the template should be available from the template dropdown under the advanced menu of a crx block.
This info came from a gh issue of crx. This is e pretty recent addition and the dev mentioned that they are looking to make this easier. So this might change in the future, this worked for me on 26/01/2023.
I have created a test vlog application using django/python and django-taggit.
The vlog and tags are working as they should.
However, I want to list all the existing tags in the django-admin interface for new and updated vlog entries - similar to displaying the tags as a filter on the django-admin list page.
This will make the selection of new or existing tags for each new or updating vlog entry that much easier.
Here is what I mean by adding the tags as help-text on the tags input field:
My models.py code:
class VlogDetails(models.Model):
....
vlog_video_image_url = models.URLField(null=False, blank=False, max_length=250, unique=True, help_text='http://img.youtube.com/vi/You_Tube_URL/0.jpg')
....
tags = TaggableManager(help_text='List all the available tags here.')
Here is the django-admin input form interface:
Is it possible to display the existing tags in another manner on the new / edit django-admin page?
I have already listed the existing / existing tags on the django-admin list page as a filter as shown below. This filter display does not display on the new / update input forms.
django-admin filter code:
class VlogDetailsAdmin(BaseDetailsAdmin):
....
list_filter = [
'vlog_date_published',
'tags',
'vlog_timestamp_added',
'vlog_timestamp_updated'
]
I have tried several things but none work and I cannot find any related ideas in the docs. Any suggestion would be appreciated. Thanks.
I will answer my own question.
I don't think that adding the tags to the new vlog page is possible - that is not possible for my level of coding experience.
However, after more investigation, I stumbled to an obscure post about django-taggit-autosuggest.
Follow the docs to install and set up the django-taggit-autosuggest.
Instead of listing all the existing tags in the help. The autosuggest option displays all the existing tags with the related letters, as shown below:
And again:
I hope that this helps someone.
So, I'm using Django-Tables to generate my project datatables, but now I'm facing a new problem.
I've got this Table Class to generate my Model datatables, using the DjangoTables app. Then I use the TemplateColumn to create a new column for base operations just like Edit, Copy, Delete... This stuff goes into the template that is loaded into the column of each row.
class ReservationTable(tables.Table):
operations = tables.TemplateColumn(template_name='base_table_operations_btn.html', verbose_name= _('Operations'))
So inside the template i've got this:
{% if perms.reservation.add_reservation %}
<span class="glyphicon glyphicon-paperclip"></span>
{% endif %}
So, using the django templates perms tags, is not working here but it does in to the normal django template.
Any tips on how can I handle those perms into this kind of template? I'm kinda losen.
Thanks in advance!
So, its not just like a "perfect answer" for this problem, but here is how I managed to solve this:
Instead of using in Template django permisions, I managed to setup the permissions in the route url config. Just by adding:
permission_required('permision_name',raise_exception=True)
Function in the url.py. So here comes the full url line:
url(r'^reservation/flight/add/$', permission_required('reservation.add_reservation',raise_exception=True)(FlightReservationCreate.as_view()), name='reservation-flight-create'),
This allow me to add perms to the View instead of filtering into the themplate view.
That's not a perfect solution, because its a different way to manage permissions, and the problem with the django-tables2 column template is still there.
By the way, the final result is the same for me, so its OK.
I'm developing a web application with Django and we have met a dilemma about the design.
We were making one template for every screen, but right now we have detected that some parts of of the screen with the information are repeated all over the different screens. For example, when coming to show personal data of a person, you can show also another data concerning that person, but not personal data (financial data, for instance).
My intuition told me that we should look for a solution in which we could make small templates that we could compose, combine and concatenate and that we should also make different views or functions which would return its own associated template each one.
Thus, person_data() would return the rendered template showing the name, surname, address, etc... and financial_data() would return the rendered template showing the salary, bank account, etc... After that, the desirable thing would be concatenating both or inserting them in a wider template for showing all this together.
<html>
...
{# Html code here #}
...
{# person_data template #}
...
...
{# financial_data template #}
...
{# Html code here #}
...
</html>
So as always I made some research on the net and I found:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
a link which describes how to use include for inserting a template file in another template:
{% include "foo/bar.html" %}
And you can also use a variable from the python function with the path name or an object with render method:
{% include template_name %}
So we could use all this in this way for combining, concatenating, composing and operating on templates. We could choose the one which includes the others and passing it the context for all templates (I suppose, I didn't test anything). But I don't know if I'm well directed or if this is the best way to go. I would appreciate some advice.
I also found another interesting thread in stackoverflow talking about this:
How do you insert a template into another template?
My idea is to have small templates which are used repeatedly in different spots of the web and composing them in Unix-like style, so I would have small visual pieces that would be used once and again saving a lot of hours of writing of code.
Please some advice.
Thanks in advance
Yes, that is a correct approach. I think the best solution is to combine {% include %} and {% extend %}, which will allow you to inherit from a base template (via extend), and include parts you want from other templates (via include).
You'd end up having a base template, a template for the header, the footer, parts of the body etc.
You might also want to read more about it here and here
I am new to django and I have I think a pretty fundamental question.
Lets say I have this theme:
I made a project already, so I know a bit about know how to build models for dynamic content, pass them to views and admin panel etc, but:
Question: on the image above I marked 3 containers that include text. There is only one instance of this text on the whole website, and it's not repeatable. If I developed for myself I would just hard-code that, but what if I develop for a client, who needs to be able to edit those fields using the admin panel?
Am I supposed to create a separate class containing multiple (lets say 20) fields for these kind of containers for the whole website, pass that class in a view (and filter with [:1]) to use it in a template?
Thats the only thing I came up with. Although it would work I think it's a terrible solution.
What I would do is write a model that contains a TextField for the blurb to insert and a CharField to identify it, and a custom template tag that reads the blurb from the database by the argument you pass to it.
class Blurb(models.Model):
ident = models.CharField(..., db_index=True)
blurb = models.TextField(...)
PK ident text
1 main Hey! Do you like this template? This...
{% load blurb %}
...
{% blurb main %}
you could have 1 model with a selection field containing a descriptor for the text in the model.
Something like:
class SomeText(models.Model):
position = models.CharField(max_length=120, choices=POSITION_DESCRIPTORS)
text = models.TextField()