Login

Most bookmarked snippets

Snippet List

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

Text Highlighting Filter

This filter can be used to wrap <span class='highlight'> around anything you want to highlight in a block of text. For example, if you had 'foo bar foo baz' inside the context variable MYTEXT, you could do {{ MYTEXT|highlight:'foo' }}, and "<span class='highlight'>foo</span> bar <span class='highlight'>foo</span> baz" would be returned. How you style the highlight class is up to you.

  • filter
  • text
  • highlight
Read More

New Pattern

Now you can use object-oriented URL resolve and include and you can add function cp_before wich will be running before any any cp__* functions in his module

  • url
  • resolving
  • object-oriented
  • newpattern
  • pattern
Read More

Restructuredtext directive for photolouge

Directive for inserting images using photolouge in django. Usage: Just make a .py with this code and import it in some apps models.py Or make a custom formatter with django-template-utils.

  • image
  • insert
  • rst
  • directive
  • photolouge
Read More

Integrate Wymeditor with filebrowser plugin

In order to integrate Wymeditor with the Django filebrowser, put the code in a file, set the fb_url variable to point to your filebrowser instance and add the file to your Javascript headers: <script type="text/javascript" src="/media/wymeditor/plugins/jquery.wymeditor.filebrowser.js"></script> or in your admin.py: class Media: js = ('/media/wymeditor/plugins/jquery.wymeditor.filebrowser.js',) Add the postInitDialog parameter to the Wymeditor initialization: $('textarea').wymeditor({ postInitDialog: wymeditor_filebrowser }); If you already have a postInitDialog function, you need to put a call to wymeditor_filebrowser inside that function. Then you should be able to click on the Filebrowser link to select an image.

  • javascript
  • images
  • wymeditor
  • filebrowser
Read More

FixedCharField and related

FixedCharField is used similarly to CharField, but takes a length attribute, and will only accept strings with that exact length. class Student(models.Model): student_id = FixedCharField(length=8) It currently only supports mysql, sqlite3, and oracle. The port to postgresql should be straightforward but I'm not sure about it so I haven't added it yet. This is a copy-paste (plus a couple of adaptations) from my project Chango, found at http://launchpad.net/chango/, so in order to keep up with latest updates it might be a good idea to use code directly from there.

  • field
  • form-field
  • fixed-length
Read More

A form field for valdating PDF and Microsoft Word document

This is form field for PDF or Microsoft Word Document (both .doc and .docx) It will validate the file uploaded as a valid PDF and MS Word Document. It extends a forms.FileField, so you can put all the arguments relevant to FileField. IMPORTANT NOTE: The method of validation is actually run thru *nix OS shell command 'file', therefore, 1. only *nix system can use this class. 2. The file uploaded must be saved on disk, meaning you need to set your upload handler to use TempoaryFileUploadHandler Only. # (i.e. put this in your settings.py) FILE_UPLOAD_HANDLERS = ( "django.core.files.uploadhandler.TemporaryFileUploadHandler", )

  • pdf
  • microsoft-word-document
  • form-field
Read More

Tamper safe HiddenFields

This snippet prevents people from tampering with the data in hidden form fields. This is something you usually want unless you have some Javascript Vodoo going on on the browser side. For the people scratching their heads: This form class will dynamically create a clean function for every passed additional hidden field, which just returns the original value of the hidden field. So the data in the hidden field posted gets actually ignored when calling the (overwritten) clean_{field name} function. This class is just an example using the protected hidden field feature for all passed field variables, which is probably not what you want. You have to add the editable fields the end of the __init__ function in the class. Example: `self.fields['bestbeer'] = forms.CharField(max_length=23)`

  • newforms
  • field
  • hidden
  • froms
Read More

update-django: Update your django git branches.

This is the (revamped) bash script I use to keep my git branches up-to-date with SVN to make my life a lot easier, just save it in a text file and read the instructions at the top! Hope it's useful to somebody else than me ;)

  • bash
  • merge
  • update
  • svn
  • git
Read More

urlquote() and urlencode() in one method

I think this method is handy, since you don't need to remember if you need urlquote or urlencode. It does both and adds a question mark between the path and the get parameters, if later are present. And it works around a bug in Django: MultiValueDicts (request.GET, request.POST) are handled correctly. Related: [http://code.djangoproject.com/ticket/9089](http://code.djangoproject.com/ticket/9089)

  • urlquote
  • urlencode
Read More

RPN template math

Django's templates don't provide much in the way of arithmetic: there is an "add" filter and that is about it. Even if sub, mult and div filters are implemented, it is difficult to chain filters while preserving some complicated expression, such as ((x+3)4-(2-y)/12.75). However, this expression can be converted into Reverse Polish Notation: x 3 + 4 * 2 y - 12.75 / - which is just a sequence of operations (push-value or apply-operator) and can be chained. To use these filters, first create a new stack for the expression with name|stnew (pass it some locally unique value). To push a number (or template variable) onto the stack, call name|stpush:number (note that you have to tell stpush the name of the stack to push onto). To pop, call name|stpop. To perform an operation, call name|st[add,sub,mult,div,mod]:number. All numbers are integers if they look like integers, or floats otherwise (integers are turned into floats upon division if they need to be). All of these functions return the name of the stack so that they can be chained. When the calculation is finished (i.e. the answer is at the bottom of the stack) call name|stget to retrieve it. Example (this was used to calculate an inline CSS value: `left: {{ forloop.counter|stnew|stpush:res.stwkday|stpush:"9.35"|stmult|stpush:res.get_item_left|stpush:"2.75"|stadd|stadd|stget }}em;`

  • template
  • filter
  • math
  • arithmetic
  • rpn
  • reverse-polish-notation
Read More

Default to current/all sites in admin (updated!)

This code sets the default sites for a sites ManyToMany property to `Site.objects.all()`, which makes sure you don't have to bother setting it for each item on a site. This could easily be changed to `Site.objects.get_current()` to use the current site as default.

  • admin
  • sites
  • default
  • site
  • current
  • all
Read More

3110 snippets posted so far.