Login

Top-rated snippets

Snippet List

extend tag with cache

By default extend tag don't cache parents template. This is extend tag with patch. Workaround for bug: http://code.djangoproject.com/ticket/6586

  • cache
  • extend
Read More

Pad integers with leading zeros (template filter)

Disclaimer: I'm not the world's greatest programmer, so there may be better ways to do this, but it works for me (feel free to offer your improvements, though!). Basically, this will pad an integer with leading zeros and return a string representation. User it like this: {% forloop.counter|leading_zeros:"5" %} ...where "5" is the number of desired digits. In this case, if it was the 12th time through the forloop, the filter would return "00012". Why do this? Either for alignment, such as in tables, or for aesthetics -- for an example, see [Shaun Inman's comment section](http://shauninman.com/archive/2007/11/16/mobilesafari_view_source).

  • template
  • filter
Read More

prevent GET or POST requests

This will return HTTP 405 if request was not POSTed. same way you can forbide POST request, change 'POST' to 'GET' Decorators provided for your convenience.

  • view
  • request
  • post
Read More

CleanCharField

I was about to start an online community but every time you allow people to post something as a comment you never know what they come up to, especially regarding profanities. So I come up with this idea, I put together some code from the old style form validators and the new newform style, plus some code to sanitize HTML from snippet number [169](http://www.djangosnippets.org/snippets/169/), and the final result is a CharField that only accept values without swear words, profanities, curses and bad html. Cheers.

  • validator
  • newforms
  • forms
  • html
  • sanitize
  • profanities
Read More
Author: DvD
  • -2
  • 0

If modulo template tag

Usage: {% ifmodulo forloop.counter 4 0 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} or {% ifnotmodulo 5 3 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} When the third parameter does not exist, it is assumed you're checking for values different than 0.

  • template
  • tag
  • if
  • modulo
Read More

Javascript HTTP response

I've been using this along with [prototype](http://www.prototypejs.org) to make simple ajax calls. In essence you make the calls from the client with... new Ajax.Request('url',{parameters:{'someparam':$('someparam').value}}); return false; On the onsubmit event of a form, onclick or whatever. Note that the return false is important to prevent the page from reloading. Sending some javascript to be executed back to the client is then as simple as setting up your view to return: return HttpJavascriptResponse('alert("boing");') So, yeah, prototype does the real work and this class does little other than make it clear what our intentions are and reduce the opportunities for typos. But it works for me.

  • ajax
  • javascript
  • prototype
  • return
Read More

fancy_if

I am working on some apps that use row level permissions. For many permission tests I intend to make custom tags so I can locate the permission test used by the template and by the view in a common module. However, frequently, before I decide what the actual end-tag is going to be I need a way to expression a more powerful 'if' structure in the template language. Frequently the existing if and if_has_perm tags are just not powerful enough to express these things. This may make the template language unacceptably more like a programming language but it has helped me quickly modify complex permission statements without having to next and repeat four or five clauses. As you can see this is intended to work with django off of the row level permissions branch. Here is an example of a template that is using the 'fancy_if' statement: ` {% fancy_if or ( and not discussion.locked not discussion.closed "asforums.post_discussion" discussion ) "asforums.moderate_forum" discussion.forum %} <li><a href="./create_post/?in_reply_to={{ post.id }}">{% icon "comment_add" "Reply to post" %}</a></li> {% end_fancy_if %} {% fancy_if or "asforums.moderate_forum" discussion.forum ( and not discussion.closed ( or eq post.author user eq discussion.author user ) ) %} {% fancy_if or "asforums.moderate_forum" discussion.forum eq post.author user %} <li><a href="{{ post.get_absolute_url }}update/">{% icon "comment_edit" "Edit Post" %}</a></li> {% end_fancy_if %} <li><a href="{{ post.get_absolute_url }}delete/">{% icon "comment_delete" "Delete Post" %}</a></li> {% end_fancy_if %} `

  • row-level-permissions
  • compound-conditional
Read More

Digg Style URL String Parser

Does a digg url effect to a string, can be useful for using an item's title in the url, from this: .hi's., is (a) $ [test], will it "work"/ \ to this: his_is_a_test_will_it_work I understand this isn't a very well made script, I am not very good at string manipulation. But I would be happy if someone would recode it in a faster, more managable way. I recomend saving the rendering.

  • url
  • clean
  • simatic
Read More

RequestMiddleware

Add RequestMiddleware to your MIDDLEWARE_CLASSES settings Then, when you need request in special cases, call get_request(), which returns the request object. This has to be used in very special cases.

  • middleware
  • threading
  • request
  • local
Read More

Load local settings

If you have a production, staging, testing and development environment, you might want to have a global checked in (in your version control system) settings.py for production + a local settings.py to override various settings (like database connection). It's also good for development, since developers don't - by incident - commit to the production settings.py, since they can use their local settings, that should be ignored (.cvsignore, .svnignore or similar).

  • settings
Read More

Login + logout script from a nub

A simple way to log in and log out users. It's most likely nowhere near as good as the built-in login forms. This was more of a learning experience for me. Nevertheless, the script works. Usage: def some_view(request): check_login(request) Some explanation can be found here: http://bit.ly/ete0

  • forms
  • login
Read More

Allow editing of the selected object of the filter_vertical/filter_horizontal widget with jquery

This patch allows to open a popup to edit the selected object of either the left or right select input of a filter_horizontal or filter_vertical field, when pressing the "Insert" key. Being a noob with general client-side issue, it's served as a patch that suits my needs. Maybe it's possible to do it cleaner by overloading the javascript methods, or get rid of the jquery dependency in order to integrate it into django trunk ... Any contribution is welcome! It's licensed under WTFPL license. Requires jquery.

  • admin
  • jquery
  • widget
  • filter_horizontal
  • filter_vertical
Read More

3110 snippets posted so far.