Login

Top-rated snippets

Snippet List

Profanity Check

Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc. This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.

  • tag
  • profanity
Read More

default url routing and shortcut

from url_helper import execute, url_ import views urlpatterns += patterns('', url(r'^(?P<urls>.*)', execute, {'views': views}), ) url_(r’/space/:username/:tag/’, views.url_), equal url(r’^space/(?P[^/]+)/(?P[^/]+)/$’,

  • shortcut
  • url
Read More

UnicodeReprMixIn

Add's updated and created fields to a model if mixed in. Example that uses the name as the representation: class Company(models.Model, UnicodeReprMixIn): """ A representation of a comic book company. """ name = models.CharField(max_length=255) slug = models.SlugField() logo = models.ImageField(upload_to=os.path.join('upload', 'company_logos')) url = models.URLField(verify_exists=True) _unicode = "name"

  • model
  • unicode
  • orm
  • mixin
  • __unicode__
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

Get stories by category and some date range

Removed for now... Not sure where to share new Ellington snippets due to the gray-area of it being proprietary. It's an awesome package but it's hard to find any good help with Django 91 code. Good luck ellingtoners!

  • date
  • stories
  • category
Read More

SnippySnip

** Help me get better! If you vote (either way) please leave a comment if you have time and say what was good or bad. I appreciate any and all feedback. Thanks! ** I keep finding places in my apps where I need an isolated snippet of text that can periodically be changed from the admin interface. Most often it's html but sometimes it's text, javascript, or css. Use it like so: (Assuming this snippet lives in snippy_snip/models.py and there is a snippet named "Welcome Message" in the database) from snippy_snip.models import snip msg = snip("Welcome Message") Or, you might populate a parameter hash for a template: def showpage(request): params = { 'welcome': snip('Welcome Message'), 'video1': snip('Video 1'), 'NavHeader': snip('Nav.SectionHeader'), } return render_to_response("main.html", params) For clarity, *params* might look something like this: welcome -> "Welcome to our site. Please use the menu on the left..." video1 - > a YouTube snippet NavHeader -> Some HTML which comprises the top of a navigation menu. This is a very simple bit of code but I've found it very useful. It isn't intended for instant changes... Your snippets will cache like anything else, which may cause confusion if you expect immediate changes. And it's probably not great for a high traffic site, but for my moderate traffic sites and workgroup apps I've found it useful. (This code was created for 0.96, but I'm working to bring it into alignment with the latest svn version of Django, see comments.)

  • text
  • javascript
  • html
  • snippet
Read More

auto_now using signals

There has been some discussion about removing `auto_now` and `auto_now_add` some time ago. `auto_now_add` can be replaced be using a callable default value, `auto_now` can't. So I wrote this litte function for my current project (older ones still use `auto_add`) to fill the gap...but I'm not sure if `auto_now` will be removed at all.

  • datetime
  • model
  • auto_now
Read More

3rd Party App Directories

How to make this work: * Put the code into your settings.py (or even better, local_settings.py if you have one) * Put 3rd party apps you'd like to use into the directories specified in APP_DIRS. * Place 'appname', into INSTALLED_APPS. Just a little trick I use to keep from having to install apps via setup.py; I can just stick apps into arbitrary locations and use them. I also do this to keep single copies of 3rd party apps I use in multiple projects (but again without installing to the global python path). No idea if this is bad practice or anything, but I find it useful.

  • settings
  • apps
  • applications
Read More

pyserver -- runserver alias

I use Django's built-in development server all the time, but get tired of typing out the command to run it, and if I'm testing some custom admin css/js, I *really* get tired of typing out the command that correctly directs the middleware to use my admin media dir. So I added this alias to my .bashrc and when I'm in a project's root, I just type 'pyserver' and the development server fires up, automatically passing an --adminmedia arg if I have some custom admin media for the project (otherwise it's 'runserver' as always). Note: This relies on the convention that my custom admin media will be in a folder called admin_media that resides at the project's base. Of course, change it to whatever convention you use. I'm not a bash expert or anything, but this works for me ;)

  • bash
  • runserver
Read More

HTML Widgets

A simple widget library to provide HTML4 widgets for HTML validation fanatics.

  • html
  • widget
  • html4
Read More

3110 snippets posted so far.