Login

Most bookmarked snippets

Snippet List

Custom model field for mysql time type.

Django does not have a suitable model field can process time type in mysql, the DateTimeField built-in django can not process more than 24 hours. That's why the code born. **Simply usage** `from myapp.models import TimeAsTimeDeltaField, SECONDS_PER_MIN, SECONDS_PER_HOUR, SECONDS_PER_DAY class EstimatedTime: days = None hours = None minutes = None seconds = None class Case(models.Model): estimated_time = TimeAsTimeDeltaField(null=True, blank=True) def get_estimated_time(self): estimated_time = EstimatedTime() if self.estimated_time: total_seconds = self.estimated_time.seconds + (self.estimated_time.days * SECONDS_PER_DAY) days = total_seconds / SECONDS_PER_DAY hours = total_seconds / SECONDS_PER_HOUR - days * 24 minutes = total_seconds / SECONDS_PER_MIN - hours * 60 - days * 24 * 60 seconds = total_seconds % SECONDS_PER_MIN estimated_time.days = days estimated_time.hours = hours estimated_time.minutes = minutes estimated_time.seconds = seconds return estimated_time else: return estimated_time

  • models
  • orm
  • database
  • field
  • timedelta
Read More

middleware for user_passes_test

Middleware to decorate views with user_passes_test in a centralized, url-matching manner. Makes it easy to apply permissions across large sections or all of a site.

  • auth
  • permissions
  • decorators
Read More

very archive view

I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name. Like so: ` 2009 October My last example blog entry Entry even before the last one First of October September Only one entry in Sept. 2008 December It is cold in December First posting of my blog ` I could not think of a way to do this using generic views, before I wrote a view and some template logic that does. The template logic is here: [http://www.djangosnippets.org/snippets/1765/](http://www.djangosnippets.org/snippets/1765/) Any suggestions for design patterns are greatly appreciated.

  • archive
  • blog-entry
Read More

very archive template

I tried to think of a way to use generic views to show all my blog posts on one page organized chronologically with each post title under one month name and all the month names under one year name. Like so: ` 2009 October My last example blog entry Entry even before the last one First of October September Only one entry in Sept. 2008 December It is cold in December First posting of my blog ` I could not think of a way to do this using generic views, before I wrote a view and some template logic that does. The view code is here: [http://www.djangosnippets.org/snippets/1766/](http://www.djangosnippets.org/snippets/1766/) Any suggestions for design patterns are greatly appreciated.

  • archive
  • blog-entry
Read More

Rails-like environments using Django

This is a replacement for settings.py, which moves the actual settings files into a directory called "env" and establishes different versions for different settings of the environment variable DJANGO_ENV. At runtime, the specified development environment can be found and loaded into the local context of settings.py, which is then picked up by whatever routine manage.py is kicking off.

  • django
  • rails
  • environment
Read More

Get sorted items for any model in any order

This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model. it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated. Additionally it write the count of the model to the context

  • template
  • tag
  • dry
Read More

Field value as plain text which can't be edited by user

Shows field value as plain text which can't be edited by user. Field value (or key value for foreign keys) is stored in hiddden input. Value of field is stored in hidden input and current value is placed as plain text. User can't change it's value. If field is foreign key then additional attribute 'key' should be set to True (key is stored in hidden field and unicode value is visible): self.fields['my_field'].widget = HiddenInputWithText(attrs={ 'key' : True }) There is one condition: for foreign key field its name have to be same as lowercased model name. Default 'key' value - False is correct for non-foreign key fields: self.fields['my_field'].widget = HiddenInputWithText()

  • forms
  • hidden
  • widget
  • input
Read More

typo_comparison

This is simple validation weather a string is close enough to what we want. First param is keyword we are comparing to Second is user's input and third is tolerance level. Its very rudimentary. I have my mind fixed upon some imperfections. I am trying to make it good for human like(crazy keyboard) error.

  • comparison
  • errors
  • typo
  • validate
Read More

cache_page that does nothing

When debugging/developing you want to be able to refresh your views every time you make a little change. But when in production mode you might want to cache these views because they contain long and resource hungry calculations or something. By putting this above "hack" in after importing `cache_page` you only cache the views in production mode.

  • cache
  • decorator
  • cache_page
Read More

improved sortby template tag

Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr" Example usage: {% for entry in entries|sortby:'category.title' %} Based on [1609](http://www.djangosnippets.org/snippets/1609/)

  • template
  • tag
  • dictsort
Read More

Testing with unmanaged models

Many of my projects heavily depend on other non-django projects to create the databases. To simplify setting up a test environment, I modified the simple test runner so that it will treat all models as managed. This will also allow for easier test set up against models that point to views. You can directly populate the view with the specific data needed for the tests, instead of populating (potentially) several models.

  • model
  • test
  • managed
Read More

3110 snippets posted so far.