django-template-filters

Django _set.all filter not working in template

依然范特西╮ 提交于 2020-07-22 22:03:06
问题 I'm trying to filter a list of objects in my database but I can't get it to work on the template using _set.all . The strange thing is it's something I've done in two other places in my project but I cant see why it isnt working this time. view.py: class GolfMonthlyView(generic.ListView): template_name="monthly_view/golf-monthly-view.html" context_object_name='golf_monthly_view' queryset = GolfMonthlyView.objects.all() def get_context_data(self, **kwargs): context = super(GolfMonthlyView,

How to prevent Django basic inlines from autoescaping

两盒软妹~` 提交于 2020-01-30 05:52:16
问题 The Django Basic Inlines app renders a pre-determined template from a pseudo-HTML syntax, based on an app/model/id combination. For example, if you're writing a blog post, you can insert an image that was saved in your image model: # In the admin This is the body of my post. <inline type="media.image" id="1" class="full"> The template then takes a render_inlines filter, which requires to be marked safe so as to render the HTML properly: # Template {{ post.body|render_inlines|safe }} But even

How To Use Django Cycle Tag

情到浓时终转凉″ 提交于 2020-01-02 03:36:05
问题 This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but don't know they mean by 'row1' and 'row2' : {% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> ... </tr> {% endfor %} Secondly, how would I correctly implement it into my template? I am using a very simple JS library, which will allow the sorting: page.html {% if Variable %} <table class

Django float format get only digits after floating point

旧街凉风 提交于 2019-12-30 11:34:40
问题 Is there a django template filter to get only digits after the floating point? For example : 2.34 --> 34 2.00 --> 00 1.10 --> 10 I have not found an answer in https://docs.djangoproject.com/en/dev/ref/templates/builtins/. 回答1: Aside from creating your own custom filter, you can solve it using django-mathfilters package: {{ value|mod:1|mul:100|floatformat:"0" }} where: mod is a "modulo" filter provided by mathfilters mul is a "multiplication" filter provided by mathfilters floatformat is a

template filter to trim any leading or trailing whitespace

…衆ロ難τιáo~ 提交于 2019-12-29 07:22:05
问题 Is there a template filter in django that will trim any leading or trailing whitespace from the input text. Something like: {{ var.example|trim }} 回答1: You can do it yourself from django import template from django.template.defaultfilters import stringfilter register = template.Library() @register.filter @stringfilter def trim(value): return value.strip() Documentation 回答2: Django templates allow you to access methods and properties by using the '.' syntax: {{ var.example.strip }} You can

Why is this template filter invalid?

放肆的年华 提交于 2019-12-25 04:09:33
问题 What am I doing wrong here? I have roundnumber in a module called accounts_extras.py located in the templatetags directory. In my template I have {% load accounts_extras %} at the top. It's also worth noting that 'upto' is currently working in another template (haven't tried it on this template yet), but the issue is with roundnumber. {{ staravg.stars__avg|roundnumber }} is giving me an invalid filter error. #accounts_extras.py from django import template from django.template.defaultfilters

update value of a variable inside a template - django

为君一笑 提交于 2019-12-24 02:33:14
问题 I have seen enough number of examples that allow me to declare a new variable inside a template and set its value. But what I want to do is to update the value of a particular variable inside the template. For example I have a datetime field for an object and I want to add timezone according to the request.user from the template. So I will create a template filter like {% add_timezone object.created %} and what it will do is that it will add timezone to object.created and after that whenever

Can custom Django filters access request.user?

爱⌒轻易说出口 提交于 2019-12-23 07:44:25
问题 Is it possible to access the current User (i.e. user in the template context) from a custom template filter? Obviously I can pass the user in as an argument, but if it's possible to just grab the current user, that would be more convenient. 回答1: Django filters aren't given any special access to the context from which they are called, they're just plain old functions. You'll need to pass in anything you want to use within the function. https://docs.djangoproject.com/en/dev/howto/custom

How to pass javascript variable to django custom filter

空扰寡人 提交于 2019-12-18 04:25:17
问题 Is there a way to access a JavaScript variable in django template code as shown below: var tags_v1 = '{{ form.tags_1.value }}'; tags_v1 = tags_v1.split('{{ form.value_delim }}'); tags_v1 = tags_v1.map(function (item) { return '{{ $(item)|get_tag }}'; }) ; I want to pass the value of "item" as variable to the custom filter "get_tag". 回答1: There is an important distinction between Django template code and in-browser JavaScript that you seem to be missing: Django templates are built on the

django template filter ,use 2 or more filter like pipe

谁都会走 提交于 2019-12-12 13:28:18
问题 I want to use more than one filter on template like below: value: {{ record.status|cut:"build:"|add:"5" }} where record.status would be build:n, 0 < n< 100 but I want to add this value a base value 5. I tried above code, it only take effect on the first filter, so I did not get the value plus 5. Does django only support one filter? Thanks 回答1: First, the answer for your question "Does django only support one filter?" is that Django does support almost unlimited number of chained filters