Davide Muzzarelli » django /blog L'informatica a valore aggiunto Mon, 25 May 2015 22:41:52 +0000 en hourly 1 http://wordpress.org/?v=3.3.2 Two template filters for Django /blog/2012/01/two-template-filters-for-django/ /blog/2012/01/two-template-filters-for-django/#comments Fri, 20 Jan 2012 11:46:23 +0000 Davide Muzzarelli /blog/?p=254 I want to signal two new template filters for Django.

Precise truncate words by chars

It truncates the text when it exceeds a certain number of characters and deletes the last word. Adds ‘…’ at the end of the text, only if truncated.

The difference between other similar filters is that this deletes the last word only if partial, so it is more precise and pleasant.

The snippet is here.

Divide a list into exact columns

It divides a list into an exact number of columns.

The difference between other similar filters it that the number of columns is guaranteed, even if there are not enough elements to fill all the columns.

The snippet is here.

]]>
/blog/2012/01/two-template-filters-for-django/feed/ 0
Create an image model for your photos in Django with multiple categories /blog/2006/07/create-an-image-model-for-your-photos-in-django-with-multiple-categories/ /blog/2006/07/create-an-image-model-for-your-photos-in-django-with-multiple-categories/#comments Tue, 18 Jul 2006 21:45:00 +0000 Davide Muzzarelli /blog/2006/07/create-an-image-model-for-your-photos-in-django-with-multiple-categories/ I write this post because Django documentation is not exhaustive for the ImageField.

Create an application in your project with:

$ python manager.py startapp photos

You can use what application name you like.

Then create the directory “photos” in your media directory.

Enter in the application directory and edit the models.py file:

from django.db import models

class PhotoCategory (models.Model):  name = models.CharField(maxlength=32)

  def __str__ (self):      return self.name

  class Admin:      pass

  class Meta:      verbose_name_plural = 'photo categories'

class Photo (models.Model):  description = models.CharField(maxlength=128)  pub_date = models.DateField('Publication date', core=True, auto_now_add=True)  category = models.ManyToManyField(PhotoCategory, verbose_name=_('photo categories'), blank=True, filter_interface=models.HORIZONTAL)  printable = models.BooleanField(help_text='Is this image for publishing aims?')  restricted = models.BooleanField(help_text='Is this image only for selected people?')  image = models.ImageField('Image file in hi resolution', upload_to="photos", height_field='height', width_field='width', core=True, help_text='Use the highest resolution possible.')  height = models.IntegerField(default=None, null=True, blank=True, help_text = "Image height in pixels (automatically filled in for you)")  width = models.IntegerField(default=None, null=True, blank=True, help_text = "Image width in pixels (automatically filled in for you)")

  def __str__ (self):      return self.normal_image + ': ' + self.description

  class Admin:      list_filter = ['normal_image']      search_fields = ['description', 'normal_image']      date_hierarchy = 'pub_date'      list_display = ('normal_image', 'description', 'pub_date')

  class Meta:      verbose_name_plural = 'photos'

Save the file and run these commands:

$ python manage.py syncdb

Django will create for you tables and permissions.

Then run:

$ python manage.py runserver

and access to the administration panel to add your photos!

All photos will be stored in your media/photos/ directory. Files with the same name will be renamed automatically.

Note that there is the #1537 bug: “ImageField height_field and width_field does not update after changing the image”

]]>
/blog/2006/07/create-an-image-model-for-your-photos-in-django-with-multiple-categories/feed/ 0