I have problem with one idea (dynamic sort template in django)
Now I have simply solution for sorting my data but it is not so good because I have couple of buttons which adnotate to sorting view and in this way I have sorted list of my data.
I tried to use angularjs, I think this is the best way to do it but I have problem too. For example:
<div ng-app="" ng-init="sort_by = 'updated_at'">
<button ng-click="sort_by = 'id'">id</button>
<button ng-click="sort_by = 'created_at'">created_at</button>
<button ng-click="sort_by = 'issuer'">issuer</button>
<button ng-click="sort_by = 'handler'">handler</button>
<p>{% verbatim angular %} {{sort_by}} {% endverbatim angular %}</p>
{% for fault in faults|dictsort:{{sort_by}} %}
in theory it can works. but not.
I try in this way and in apostrophe and I try with another block like above {% verbatim angular %} and this is not working giving me an error like this:
'for' statements should use the format 'for x in y': for fault in faults|dictsort:'{% verbatim angular
so it is even not recognizing angular block.
I have to ask You for help and it doesn't have to be angular, if it is better idea to do this in template or maybe stricte django way??
Thanks for help me :)
Why you type ng-app="" ? You should put here name of your AngularJS application. Did You create your AngularJS module first?
You can sort both in frontend and backend, but You should do this in frontend - it is the job of web browser - backend should be responsible only for generating data to be consumed for frontend.
Something like this {% for fault in faults|dictsort:{{sort_by}} %} can't work. You try to define an AngularJS variable called sort_by, and then use it in Django filter. Django doesn't know nothing about that variable.
Both Django and AngularJS use {{}} for variable bindings in templates. You can change these symbols for AngularJS or use verbatim and endverbatim tag in Django.
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 am trying to achieve something in Django Templating Language without knowing whether it is possible (without resorting to JS or something like that).
I have the following code (from Django-Oscar) which renders 3 blocks of promotions on the main page:
{# Render promotions #}
<div id="promotions">
{% for promotion in promotions_page %}
{% render_promotion promotion %}
{% endfor %}
</div>
These three blocks are Single Item, Products and New Arrivals. The code above appears in the layout.html which is essentially responsible for rendering the entire layout of the main page.
The problem is that as soon as soon as Django encounters this code, it renders all of the promotions there one after another. I, however, would like to chose where on the page I place them. What's more, I do not believe that I have much flexibility in how I render them, read - I do not want to change the Oscar e-commerce and rendering code unless absolutely necessary.
Since I have access to individual templates for Single Item, Products and New Arrivals promotions, I tried creating DTL blocks there and then calling those blocks at proper places on the layout.html. However, that does not work.
What is a proper and most efficient way of achieving what I am trying to do?
Django templating system provides features that let you add your custom templates in place of the ones that come with the apps. You don't need to patch django-oscar for this.
Start here: https://docs.djangoproject.com/fr/1.11/howto/overriding-templates/
Hope I added enlightenment.
I have an API built with django-rest-framework, and I'm looking to add filtering to it. I'm following the guide here:
http://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend
I'm seeing two issues:
the generated form uses method="post". My API currently supports only GET requests, and applying the filter results in "405 method not allowed" error.
The generated form has no submit button, but the example screenshot does.
my module versions:
Django (1.8)
django-crispy-forms (1.6.0)
django-filter (0.12.0)
django-guardian (1.3.2)
django-mssql (1.7)
django-pyodbc (0.3.0)
django-rest-swagger (0.3.4)
djangorestframework (3.3.2)
I've dug into the templates and it appears theres no easy way to override the form creation. base.html template in rest framework has this at the end
{% if filter_form %}
{{ filter_form }}
{% endif %}
but its contained in the {% block body %}, so to override it with my own template i'd have to also include everything else in the body. This seems lie the wrong solution.
I've also searched through the documentation looking for a place to specify GET instead of POST, and haven't found any mention of this.
I don't want to change the API to handle filters through the POST method. Eventually API will be extended to support a real POST method for actually creating new resources, so applying a filter via POST now would just create other problems down the road.
Is there a way to change the submit method of the modal form? Or a good way to override it with my own modal template without duplicating the rest of the body block?
================
EDIT
I've managed to correct the above issues using some jQuery, but this feels rather hackish. If a better solution is out there I'd still be interested in it.
$(document).ready(function () {
// change filter submit method to GET instead of POST
var form = $('#filtersModal form');
form.attr('method', 'get');
// add a submit button to the filter modal dialog
var cancel = $('<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>');
var submit = $('<button type="submit" class="btn btn-primary">Apply</button>');
var footer = $('<div class="modal-footer"></div>');
footer.append(cancel);
footer.append(submit);
form.append(footer);
});
I guess this is a bit late reply.
Extend your filter class from
django_filters.rest_framework.FilterSet
instead of
django_filters.FilterSet
Solution described here
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 working with Django and in the base template I see this: {% get_cms_html_fragment "html/footer" %}
From the surrounding syntax I can easily conclude that this is a footer include of some sort. But, it doesn't follow the traditional syntax of {% block headerexample %}{% endblock %} that is normally used in Django templates. Does anyone recognize this?
In Django you can easily create custom tags. So it can be this very project specific tag.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
This is a syntax for a template tag. FYI, block is one of the built-in template tags, get_cms_html_fragment is a custom one.
Custom template tags and filters are loaded via load built-in template tag.
See also:
Django: what does "load" do (in a template file)?