Snippet List
I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly.
So i wrote this little function to cut the center of a string i thought it cute so i leave it here.
Pay attention to the comment so you can see what is going on.
This will truncate a long character based on the given length parameter. If the word is cut-off, it will return the string up until the next space. If there are no spaces in the next 5 characters, that should mean a very long word and we should truncate right away.
Simple filter that truncates string to specific number of letters. Example usage in template:
`{{ myvariable|truncatestring:20 }}`
if myvariable is "That is my long string", the result will be: "That is my long s...".
Put the code into templatetags/.
Some template tags/filter for working with query strings in templates.
Examples:
{% load qstring %}
{% qstring %} # Prints current request's query string
{% qstring as current_qstring %} # Same but goes to context
{{ current_qstring|qstring_del:"key1" }} # Deletes all key1 values
{{ current_qstring|qstring_del:"key1&key2" }} # Deletes all key1 and key2values
{{ current_qstring|qstring_set:"key1=1&key2=2" }} # Deletes all old key1 and key2 values and adds the new values.
[The Session documentation](http://www.djangoproject.com/documentation/sessions/) rightly warns of the dangers of putting a session ID into a query string. Sometimes, however, you have to do it - perhaps your client has mandated support for browsers with cookies disabled, or perhaps (as in my case) you're just dealing with a slightly broken client browser.
This middleware pulls a session ID out of the query string an inserts it into the cookies collection. You'll need to include it in your MIDDLEWARE_CLASSES tuple in settings.py, *before* the SessionMiddleware.
*Please* read my [full blog post](http://www.stereoplex.com/two-voices/cookieless-django-sessions-and-authentication-without-cookies) about for the dangers of doing this, and for full instructions and examples.
- middleware
- cookie
- session
- authentication
- string
- query
- cookieless
- querystring
This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting.
The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T".
As a 'dumb' illustration, the following template tag usage:
It is {% now "jS F Y H:i" %}
...could equate to the following:
It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script>
It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.
- javascript
- dynamic
- datetime
- date
- format
- time
- string
- now
14 snippets posted so far.