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.
Related
I'm new to django, i'm used to angular. i'm trying to do something that make sense to me in angular and I can't achieve in django.
I'm working with python 3.9 and django 4.1
I simplified my case to this..
I have a form that I created and and a view for it, i have a select element, whenever i select something, i want to show what i selected.
so I created a LocationForm form class:
class LocationForm(forms.Form):
apartment_type = forms.ModelChoiceField(queryset=ApartmentType.objects.all())
apartment type is just a list of apartment types (building, apartment, garden and so on)
i past the form to the view:
def location(request):
context = {'form': LocationForm}
return render(request, 'prospects/location.html', context)
and the code for the view:
{% load static %}
<form>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
CCC{{ form.apartment_type.value }}DDD<br/>
the problem is that no matter what I select, form.apartment_type.value still shows None, it's not being updated.
i guess i'm used to angular too much but this issue is really alarming for me if django only parses things statically, i guess i'm not used to it and i have no idea how to resolve it otherwise.
in my full code i want to reflect different type of items based of what chosen in the form but i can't do that if nothing gets updated.
any ideas how to resolve this?
any information regarding this issue would be greatly appreciated, i'm really lost here.
#update
it looks like i wasn't clear.
I want to understand the django template updates when variables are changed inside of it without refreshing the page.
only when i change the selection, without clicking save and sending the form i want to see the new value printed between CCC and DDD. currently i'm using {{ form.apartment_type.value }} but it stays None when i select an item.
if not.. how can i resolve this with django ?
i just said that i moved from angular and there it's like that out of the box and if here it's not, i hope there is a solution.
It seems that Django only compiles the pages when I render them first, if want to change anything in realtime I need to a javascript framework.
Found that at: https://stackoverflow.com/a/50189643/
I was actually trying to find a way to sort a queryset in my template and saw that we can create our custom filter and use it.
let's not to mention the function I put into my tag but I created the tag in the same directory as all other tags...
I created my tag name sort_tags.py under templatetags
*weird I cannot upload image, was going to show my directories`
Anyways, that's where all other tags are.
I got this piece of code and want to try it..so I paste it into my sort_tags.py
from django import template
register = template.Library()
#register.filter
def sort_by(queryset, order):
return queryset.order_by(order)
then in my template.html I used {% load sort_tags %} also, there are other tags being loaded too.
Then when I load the page...is not a registered tag library. must be one of shows up. in the list of MUST BE, I see all the tags in the same directory as me.
I tried something like filter_function, register_filter('sort_by',sort_by)
or something similar, found a few different ways, so I tried them all and none of them work.
Does anyone has any idea what might be wrong?
Thanks in advance
If you want to do this inside a template (which is a bit wrong actually), you should be using the regroup tag — https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#regroup
But actually, you should move the logic to the view as much as possible.
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 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
I find myself needing a bit more flexibility than what I understand I can do based on the Django documentation for reversing Admin URLs. I'm doing things like:
{% url admin:billing_creditcardtoken_add %}?customer={{ user.id }}
This works, but it feels like I should be able to do it without leaving the template tags.
If I want to find all CreditCardToken objects from the billing application that belong to the current user, I find myself doing:
{% url admin:billing_creditcardtoken %}?customer={{ user.id }}
...but this fails altogether. Is there a more elegant way of getting these URLs?
I was looking at this the wrong way. While:
{% url admin:billing_creditcardtoken_add %}?customer={{ user.id }}
...might be somewhat ugly, the only thing added syntax would serve to do is try to construct a query string, which isn't something one reverses URLs to do normally anyway. So this is an acceptable method of accomplishing this task.
What I was looking for in the second turned out to be:
{% url admin:billing_creditcardtoken_changelist %}?customer={{ user.id }}
...changelist, as it turns out, does not show a history of changes, but creates a list of possible items to change. Adding the query string applies the proper filter I needed.