Login

Snippets by jayliew

Snippet List

Filter to add zero-width space to break up long words

If you have long words (no spaces) that are so long that it's messing up your design, add a 0-width space in the word every X chars. Usage: Step 1. Inside your app's directory, create dir called 'templatetags'. In that directory, create a .py file (say 'app_extras.py'). Make sure you make this a python module, make an empty the init .py (with the 2 underscores on each side). Step 2. Inside template (make sure app is on INSTALLED_APPS list in settings.py): {% load app_extras %} Step 3. Enjoy! {{ some_long_word_with_no_breaks|zerowidthspace_separator:25 }}

  • wordwrap
  • wordbreak
  • word-break
  • long-words
  • break
  • long-lines
  • wrap
  • char-break
Read More

Querying datetime aware objects in your local timezone

I have a model with a datetime field that I used as a timestamp. I’m in California’s timezone (“America/Los_Angeles”). The data is saved in UTC in MySQL (as confirmed by the ORM). I just want to do a query that looks like this: “give me all the information with day X’s timestamp” (24 hour period). But the timestamp is a datetime, not date. If you just do varname.date(), it’s still UTC’s date, not your local timezone’s date. Here’s what I did: 1. First construct the start and end time period covering the 24 hour period of that day you want 2. Make it an “aware” (not naive) datetime 3. Filter for the __range

  • datetime
  • timezone
  • queryset
  • utc
  • local
  • datetimefield
Read More

jayliew has posted 2 snippets.