473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Django: Why cannot I use the custom file field?

11
I have been tortured by this problem for a whole week. I really hope a superman can help me solve it. I tried to create a new file field called "ThumbnailImageField". The following is my model.py:

Expand|Select|Wrap|Line Numbers
  1. from django.db import models
  2. from django.contrib import admin
  3. from cn.han import ThumbnailImageField
  4. from django.db.models.fields.files import ImageField, ImageFieldFile
  5. # Create your models here.
  6.  
  7. class Item(models.Model):
  8.     name = models.CharField(max_length=250)
  9.     description = models.TextField()
  10.  
  11.     class Meta:
  12.         ordering = ['name']
  13.  
  14.     def __unicode__(self):
  15.         return self.name
  16.  
  17.     @models.permalink
  18.     def get_absolute_url(self):
  19.         return ('item_detail', None, {'object_id': self.id})
  20.  
  21. class Photo(models.Model):
  22.     item = models.ForeignKey(Item)
  23.     title = models.CharField(max_length=100)
  24.     image = ThumbnailImageField(upload_to='photos')
  25.     #TypeError here: 'module' object is not callable.
  26.     caption = models.CharField(max_length=250, blank=True)
  27.  
  28.     class Meta:
  29.         ordering = ['title']
  30.  
  31.     def __unicode__(self):
  32.         return self.title
  33.  
  34.     @models.permalink
  35.     def get_absolute_url(self):
  36.         return ('photo_detail', None, {'object_id': self.id})
  37.  
  38. class PhotoInline(admin.StackedInline):
  39.     model = Photo
  40.  
  41. class ItemAdmin(admin.ModelAdmin):
  42.     inlines = [PhotoInline]
  43.  
  44. admin.site.register(Item, ItemAdmin)
  45. admin.site.register(Photo)
  46.  
The following is my ThumnailImageField.py which stored under /cn/han/:

Expand|Select|Wrap|Line Numbers
  1. from django.db.models.fields.files import ImageField, ImageFieldFile
  2. from PIL import Image
  3. import os
  4. from django.db import models
  5.  
  6. def _add_thumb(s):
  7.     """
  8.     Modifies a string (filename, URL) containing an image filename, to insert
  9.     '.thumb' before the file extension (which is changed to be '.jpg').
  10.     """
  11.     parts = s.split(".")
  12.     parts.insert(-1, "thumb")
  13.     if parts[-1].lower() not in ['jpeg', 'jpg']:
  14.         parts[-1] = 'jpg'
  15.     return ".".join(parts)
  16.  
  17. class ThumbnailImageField(ImageField):
  18.     """
  19.     Behaves like a regular ImageField, but stores an extra (JPEG) thumbnail
  20.     image, providing get_FIELD_thumb_url() and get_FIELD_thumb_filename().
  21.  
  22.     Accepts two additional, optional arguments: thumb_width and thumb_height,
  23.     both defaulting to 128 (pixels). Resizing will preserve aspect ratio while
  24.     staying inside the requested dimensions; see PIL's Image.thumbnail()
  25.     method documentation for details.
  26.     """
  27.  
  28.     def __init__(self, thumb_width=128, thumb_height=128, *args, **kwargs):
  29.         self.thumb_width = thumb_width
  30.         self.thumb_height = thumb_height
  31.         super(ThumbnailImageField, self).__init__(*args, **kwargs)
  32.  
  33.     def _get_path(self):
  34.         self._require_file()
  35.         return self.storage.path(self.name)
  36.     path = property(_get_path)
  37.  
  38.     class ThumbnailImageFieldFile(ImageFieldFile):
  39.         def _get_thumb_path(self):
  40.             return _add_thumb(self.path)
  41.         thumb_path = property(_get_thumb_path)
  42.  
  43.         def _get_thumb_url(self):
  44.             return _add_thumb(self.url)
  45.         thumb_url = property(_get_thumb_url)
  46.  
  47.         def save(self, name, content, save=True):
  48.             super(ThumbnailImageFieldFile, self).save(name, content, save)
  49.             img = Image.open(self.path)
  50.             img.thumbnail(
  51.                 (self.field.thumb_width, self.field.thumb_height),
  52.                 Image.ANTIALIAS
  53.             )
  54.             img.save(self.thumb_path, 'JPEG')
  55.  
  56.         def delete(self, save=True):
  57.             if os.path.exists(self.thumb_path):
  58.                 os.remove(self.thumb_path)
  59.             super(ThumbnailImageFieldFile, self).delete(save)
  60.     attr_class = ThumbnailImageFieldFile
  61.  
Finally here is my template called items_list.html:

Expand|Select|Wrap|Line Numbers
  1. {% extends "base.html" %}
  2.  
  3. {% block title %}Item List{% endblock %}
  4.  
  5. {% block content %}
  6.  
  7. <p><a href="{% url index %}">&laquo; Back to main page</a></p>
  8.  
  9. <h2>Items</h2>
  10. {% if object_list %}
  11. <table>
  12.     <tr>
  13.         <th>Name</th>
  14.         <th>Sample Thumb</th>
  15.         <th>Description</th>
  16.     </tr>
  17.     {% for item in object_list %}
  18.     <tr>
  19.         <td><i>{{ item.name }}</i></td>
  20.         <td>
  21.             {% if item.photo_set.count %}
  22.             <a href="{{ item.get_absolute_url }}">
  23.                <img src="{{ item.photo_set.all.0.image.thumb_url }}" />
  24.             </a>
  25.             {% else %}
  26.             (No photos currently uploaded)
  27.             {% endif %}
  28.         </td>
  29.         <td>{{ item.description }}</td>
  30.     </tr>
  31.     {% endfor %}
  32. </table>
  33. {% else %}
  34. <p>There are currently no items to display.</p>
  35. {% endif %}
  36.  
  37. {% endblock %}
Mar 30 '12 #1
0 1793

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: atse | last post by:
Hi, In the file field <input type="file" name="file1">, can I modify the button "Browse..."? I want to hide the file field but show a button like "Open". When I click on this button, it catches a...
1
by: Phil Price | last post by:
Hi there, I'm developing a shape recognition application for the tablet PC for a) fun b) university project. Currently I'm working on the learning stage using neural networks, and have to store...
1
by: Monte Chan | last post by:
Hi all, I have the following codes, <script language="JavaScript"> function check_stuff(field) { alert("blank out the field now"); field.value = ""; } </script>
10
by: Eddy | last post by:
I am coding a ASP to process a excel file and output the reults to a text file. To browse for the file that i want to process i used the File field to return the location of the file. My problem...
1
by: VB Programmer | last post by:
I have a custom file type ("PWA" extension.) It's just a text file with some text created by the web server. When my web page redirects to one of these pages I want to auto launch a local app. ...
0
by: davesil2 | last post by:
I am using the File Field HTML Control in my asp.net page. I believe what I need to do is use a custom validator against that control to check if the file size is too large. I don't know where to...
3
by: Paul | last post by:
I need a way of creating a custom file which can be downloaded by a user. The file needs to be customized pre user (ie serial number built in). Creating the file is no problem. The problem is how...
6
by: msuk | last post by:
All, I have a ASP.NET/C# webform which contains a Webform button that has a CssClass applied to give it a special effect. Now the same webform has a filefield control but the browse button is...
1
exoskeleton
by: exoskeleton | last post by:
Hi..to all expert...i have this situation .. i cant get the value of the file field in the other page..im using a simple javascript like this... example: // this is page1.php <head> ...
2
by: Tim Haughton | last post by:
In a styled WPF application, I need to write a custom file browser in C#/WPF. As it is styled differently to generic Windows apps, I cannot use the standard open file dialogs. Writing a tree or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.