Django: display a preview of an object's attribute - Class based views - django

Using a ListView Class-based-view, I am looping over the objects present in the database of a certain model in my HTML template, and, for instance, I can access an object's "body_text" attribute with the following syntax: {{object.body_text}}
What if I wanted to only show the first 20 characters of that "body_text" attribute in my HTML template?
How can I set that?

1st Method
Use the truncatechars filter in your HTML template.Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis character (“…”).
{{object.body_text|truncatechars:20}}
Reference:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatechars
2nd Method
Use the slice filter in your HTML template.
{{object.body_text|slice:":20"}}
Referernce: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice

Found it, eventually.
Use the |slice:":" filter in your HTML template.
For example, if you only want to display the first 10 characters of a given attribute, use:
{{object.body_text|slice:":10"}}

Related

Initial django admin forms with data

I've used django-admin and assigned a django-form to it. I need to fill up one of my custom rows with a random dynamic data. I can't use initial from form attributes BTW. I need to get the data from my URL and my URL is generated with another component.
Field values can be passed into a form by appending them to the form’s URL using the following structure:
baseURL?variable1=value
Replace baseURL with the form’s URL, variable1 with the variable associated with the field to be filled in and value with the appropriate field value. To fill in multiple fields, insert the ampersand symbol (&) before any additional variables:
baseURL?variable1=value&variable2=value

Write not fully displayed Django field

I need to write a Django custom field where the stored value is not fully displayed.
For example if the stored value is "079684" I need it to be displayed as "---84". Is it possible to do so ? If yes how should I implement my custom field ?
I have solved this by overriding from_db_value() of the custom field method.
You could store your value as a string in your model and than use slice in template to show only last two character.
{{ value|slice:"-2:" }}

Access to the model element after filtering in the template

I can not access the model field after applying the filter in the template, like this:
{{service_item_v|get_first|first|get_item:'price'.service}}
The problem is that it throws an error:
TemplateSyntaxError at /payment/
Could not parse the remainder: '.service' from 'service_item_v|get_first|first|get_item:'price'.service'
The element finds correctly if you remove the field name from the dot. Also the element has this field, since it gets it if you do not apply the filter.
The question is how to access the field in this case?
You just need to put the result of filtering through "with" under another name. And after that you can access the attributes of the element.
You can't use it on that way, it's not how it works. Because we can't see what your filter doing, I can suggest two things:
Create another filter or add more arguments to your filter get_item:'price,service'
Then in your filter split values:
def get_item(value):
values_list = value.split(",")
# do something with values from the list
create a function in your model instead of template filter and do filtering there.
def get_item(self):
price = # your logic
return price.service
Then in your template just {{ service_item_v.get_item }}

Django annotate according to foreign field's attribute

I normally use something like this "Tag.object.annotate(num_post=Count('post')).filter(num_post__gt=2)" to get tags with more than 2 posts. I want to get number of posts with a field value (e.g post.published=True) and annote over them so that I get tags with number of published posts bigger than some value. How would I do that?
Edit:
What I want is not filter over annotated objects. What I want is something like this: Tag.objects.annotate(num_post=Count("posts that have published field set to true!")). What I am trying to learn is, how to put post that have published field set to true in Count function.
You can just replace the 2 in ..._gt=2 with some other variable - for example, a variable that gets passed into the view, or a request.GET value, or similar.
Is that what you're trying to do?

Variable number of fields in Django URL

I'd like a URL of the form:
... field1/eq/value1/field2/gt/value2/ ...
Where I want to filter the contents of the page in the view function based on an arbitrary number of fields (the names of which are not known in advance).
I've tried:
(r'^((?P<field>\w+)/(?P<op>[a-z]+)/(?P<value>\w+)/)*$', my_view)
But the keyword arguments are filled with the last set of three field/op/value to occur in the URL.
Is there any way that I could populate a list or dictionary based on a variable number of URl fields like this?
Or should I be doing this some completely different way?
Don't do this.
Use the query string. ?field,eq,value&field,gt,value will work out much better.