Remove first character from string Django template

2024/6/30 11:22:55

I know this has been asked multiple times but the solution that everyone reaches (and the documentation) doesn't seem to be working for me...

Trying to remove first character

Code is {{ picture.picture_path|slice:"1:" }}

but it still comes out as ./DOF_mrD5T49.jpg. Trying to get of the leading dot. Is it possible that I can't remove it because it's a the "name" of picture_path?

Pertinent model code:

class Picture(models.Model):picture_path = models.ImageField(blank=True)def __str__(self):return self.picture_path.name
Answer

This should work:

{{ picture.picture_path.name|slice:"1:" }}

The reason your first attempt didn't work is that picture.picture_path represents a FieldFile object rather than a string. This is what gets passed to the slice filter.

The slice filter fails silently if an invalid input is provided, and returns the original value that was supplied. It is only after this that Django attempts to convert that original value to a string, using the object's __str__ method.

https://en.xdnf.cn/q/73385.html

Related Q&A

Prevent Python logger from printing to console

Im getting mad at the logging module from Python, because I really have no idea anymore why the logger is printing out the logging messages to the console (on the DEBUG level, even though I set my File…

How to remove python assertion when compiling in cython?

so, here is my problem: I code in python, but I need to improve performance in some part of my code that are too slow. A good(and easy) solution seems to be using cython; I tried it and got good result…

ignoring newline character in regex match

I am trying to replace all matching occurrences with title cases using the following script. When there is a newline character between filter words (in this case ABC and DEF) that line doesnt get repla…

Xml parsing from web response

Im trying to get response from nominatim to geo-code few thousands of cities. import os import requests import xml.etree.ElementTree as ETtxt = open(input.txt, r).readlines() for line in txt:lp, region…

How to Install rpy2 on Mac OS X

I am trying, so far unsuccessfully, at installing the rpy2 for python on my Mac OSX. I have tried Macports and DarwinPorts but have had no luck with import rpy2 within the python shell environment. I…

Socket.IO vs. Twisted [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

How do I make Django ManyToMany through queries more efficient?

Im using a ManyToManyField with a through class and this results in a lot of queries when fetching a list of things. Im wondering if theres a more efficient way.For example here are some simplified cla…

How do I structure my Python project to allow named modules to be imported from sub directories

This is my directory structure:Projects+ Project_1+ Project_2- Project_3- Lib1__init__.py # emptymoduleA.py- Tests__init__.py # emptyfoo_tests.pybar_tests.pysetpath.py__init__.py # emptyfoo.pybar.p…

Plotly - Adding Scatter Geo points and traces on top of Density Mapbox

I am trying to add a Scattergeo trace or overlay on top of a white-bg density mapbox to get a heat map over a generic USA states outline. The reason for my use of scattergeo is Id like to plot a star s…

Removing items randomly from a dictionary

How do I remove random items from a dictionary in Python?I have to remove a specified number of items from a dictionary and so I tried to use dict.popitem which I thought was random, but it is seems i…