Debug-only static serving
A simple way to enable static serving while in development stage still - on release, simply set up the web server to serve the static content instead, and adjust `MEDIA_URL` accordingly.
- static-serve
A simple way to enable static serving while in development stage still - on release, simply set up the web server to serve the static content instead, and adjust `MEDIA_URL` accordingly.
This is a simple filter that takes an object list and returns a comma-separated list of hyperlinks. I use it to display the list of tags in my blog entries. The function assumes the object has a `get_absolute_url()` method that returns the URL. In your template, write `{{ obj_list|hyperlink_list:"slug" }}` where `obj_list` is your object list and `"slug"` is the object's attribute that returns the link text.
**Note**: The `--failfast` argument in Django since version 1.2 does this. Use this snippet for earlier versions. If a large number of your unit tests get "out of sync", it's often annoying to scan through a large number of test failures which overflow the terminal window's scroll buffer. This library strictly stops after the first failure in a doctest suite. If you're testing multiple applications, it also stops after the first test suite with failures in it. So effectively you'll get one failure at a time. This code has been tested with doctests only so far. You can also fetch the latest source from [my repository](http://trac.ambitone.com/ambidjangolib/browser/trunk/test/).
InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them. This code uses [Ofer Faigon's](http://www.bitformation.com) topological sort to sort the models so that any models with a ForeignKey relationship are dumped after the models they reference. class Entry(models.Model): txt = .... class Comment(models.Model): entry = models.ForeignKey(Entry) This code will ensure that Entry always gets dumped before Comment. Fixtures are an important part of the django Unit Testing framework so I really needed to be able to test my more complicated models. **Caveats** 1. You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations. 2. This code does not handle Circular or self-references. The loaddata for those needs to be much smarter.
One thing I wanted for a while was the ability to basically apply something like @login_required to a bunch of urlpatterns in one go, instead of having to decorate each and every view manually. In this example, the latter two views will always raise a 404.
If you need to upload Image files into folders qualified with user name eg.: 'images/user1/2008/01/01' then you may use this snippet. In order to use it you have to install ThreadLocals middleware as described here: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Then just import UserImageField class at your models.py and specify 'upload_to' parameter with '%(user)s' in the path eg.: ` image3 = UserImageField(_('Image 3'), upload_to='flower_images/%(user)s/%Y/%m/%d', null=True, blank=True) `
I noticed that form_for_* in newforms now carry deprecation warnings. This code is a minimal demonstration of how to use ModelForm as a replacement. Full information can be found on [Stereoplex](http://www.stereoplex.com/two-voices/django-modelform-and-newforms). Hope this helps you.
This snippet shows an alternative and interesting way to do Model Inheritance in Django. For description of the code, follow the inline comments.
Wrapper for render_to_response that allows you to pass in a optional preinitialized HttpResponse object. Helpful when you want to want to set cookies or just add some extra initialization to your HttpResponse. If no HttpResponse is passed in the normal render_to_response is called. It's called exactly like the normal render_to_response except that you can pass in a kwargs response pair if you wish. Like so: code render_response('index.html',{'aparam': val}, context_instance=RequestContext(request),response=my_response)
It seems like one way or another I always need to get access to a specific field of a Model object. The current way to do this is to iterate through the object's _meta.fields list, comparing with the `name` attribute. Why not just have a lookup of fields? If you paste this code at the bottom of your models.py file it will add a `field_map` attribute to the meta options. For example: `profile = User.objects.get(id=1).get_profile()` `upload_to = profile._meta.field_map['image_icon'].upload_to` ...
Based on jspacker by Dean Edwards, Python port by Florian Schulze: http://www.crowproductions.de/repos/main/public/packer/jspacker.py Packs javascript Example usage:: {% packjs %} var a = 1; var b = 2; var c = 3; alert(a+b); {% endpackjs %} This example would return this script:: eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[c]=k[c]||c;k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp("\\b"+e(c)+"\\b","g"),k[c]);return p}('0 5 = 1;\n 0 4 = 2;\n 0 7 = 3;\n 6(5+4);\n',8,8,'var||||b|a|alert|c'.split('|'),0,{}))
In some cases we need to know if we were opened via https from template. Usage: {% ifsecure %}using https{% else %}not using https{% endifsecure %} If you use fastcgi fastcgi_param HTTPS must exists.
This is useful especially during the model-creation stage, when things are in constant flux. The `freshdb` command will drop the project's database, then create a new one. A common use case: manage.py freshdb manage.py syncdb
This is just an example, **NOT any particular tag**. I was just tiered in examining every bits in list. I converted list to dictionary for easier manipulation of parameters. You can use this keeping in mind syntax: {% tag_name 1_key 1_value 2_key 2_value %} and so on...
Custom template filter to generate a breadcrumb trail for a flatpage. Say you have a series of flatpages with URLs like /trunk/branch/leaf/ etc. This filter looks at the URL of a given flatpage, figures out which of the leftwards text chunks correspond to other flatpages, and generates a string of anchored HTML. Usage: {% load make_breadcrumb_trail %} {{ flatpage.url|crumbs:flatpage.title }}
3110 snippets posted so far.