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: - from django.db import models
-
from django.contrib import admin
-
from cn.han import ThumbnailImageField
-
from django.db.models.fields.files import ImageField, ImageFieldFile
-
# Create your models here.
-
-
class Item(models.Model):
-
name = models.CharField(max_length=250)
-
description = models.TextField()
-
-
class Meta:
-
ordering = ['name']
-
-
def __unicode__(self):
-
return self.name
-
-
@models.permalink
-
def get_absolute_url(self):
-
return ('item_detail', None, {'object_id': self.id})
-
-
class Photo(models.Model):
-
item = models.ForeignKey(Item)
-
title = models.CharField(max_length=100)
-
image = ThumbnailImageField(upload_to='photos')
-
#TypeError here: 'module' object is not callable.
-
caption = models.CharField(max_length=250, blank=True)
-
-
class Meta:
-
ordering = ['title']
-
-
def __unicode__(self):
-
return self.title
-
-
@models.permalink
-
def get_absolute_url(self):
-
return ('photo_detail', None, {'object_id': self.id})
-
-
class PhotoInline(admin.StackedInline):
-
model = Photo
-
-
class ItemAdmin(admin.ModelAdmin):
-
inlines = [PhotoInline]
-
-
admin.site.register(Item, ItemAdmin)
-
admin.site.register(Photo)
-
The following is my ThumnailImageField.py which stored under /cn/han/: - from django.db.models.fields.files import ImageField, ImageFieldFile
-
from PIL import Image
-
import os
-
from django.db import models
-
-
def _add_thumb(s):
-
"""
-
Modifies a string (filename, URL) containing an image filename, to insert
-
'.thumb' before the file extension (which is changed to be '.jpg').
-
"""
-
parts = s.split(".")
-
parts.insert(-1, "thumb")
-
if parts[-1].lower() not in ['jpeg', 'jpg']:
-
parts[-1] = 'jpg'
-
return ".".join(parts)
-
-
class ThumbnailImageField(ImageField):
-
"""
-
Behaves like a regular ImageField, but stores an extra (JPEG) thumbnail
-
image, providing get_FIELD_thumb_url() and get_FIELD_thumb_filename().
-
-
Accepts two additional, optional arguments: thumb_width and thumb_height,
-
both defaulting to 128 (pixels). Resizing will preserve aspect ratio while
-
staying inside the requested dimensions; see PIL's Image.thumbnail()
-
method documentation for details.
-
"""
-
-
def __init__(self, thumb_width=128, thumb_height=128, *args, **kwargs):
-
self.thumb_width = thumb_width
-
self.thumb_height = thumb_height
-
super(ThumbnailImageField, self).__init__(*args, **kwargs)
-
-
def _get_path(self):
-
self._require_file()
-
return self.storage.path(self.name)
-
path = property(_get_path)
-
-
class ThumbnailImageFieldFile(ImageFieldFile):
-
def _get_thumb_path(self):
-
return _add_thumb(self.path)
-
thumb_path = property(_get_thumb_path)
-
-
def _get_thumb_url(self):
-
return _add_thumb(self.url)
-
thumb_url = property(_get_thumb_url)
-
-
def save(self, name, content, save=True):
-
super(ThumbnailImageFieldFile, self).save(name, content, save)
-
img = Image.open(self.path)
-
img.thumbnail(
-
(self.field.thumb_width, self.field.thumb_height),
-
Image.ANTIALIAS
-
)
-
img.save(self.thumb_path, 'JPEG')
-
-
def delete(self, save=True):
-
if os.path.exists(self.thumb_path):
-
os.remove(self.thumb_path)
-
super(ThumbnailImageFieldFile, self).delete(save)
-
attr_class = ThumbnailImageFieldFile
-
Finally here is my template called items_list.html: - {% extends "base.html" %}
-
-
{% block title %}Item List{% endblock %}
-
-
{% block content %}
-
-
<p><a href="{% url index %}">« Back to main page</a></p>
-
-
<h2>Items</h2>
-
{% if object_list %}
-
<table>
-
<tr>
-
<th>Name</th>
-
<th>Sample Thumb</th>
-
<th>Description</th>
-
</tr>
-
{% for item in object_list %}
-
<tr>
-
<td><i>{{ item.name }}</i></td>
-
<td>
-
{% if item.photo_set.count %}
-
<a href="{{ item.get_absolute_url }}">
-
<img src="{{ item.photo_set.all.0.image.thumb_url }}" />
-
</a>
-
{% else %}
-
(No photos currently uploaded)
-
{% endif %}
-
</td>
-
<td>{{ item.description }}</td>
-
</tr>
-
{% endfor %}
-
</table>
-
{% else %}
-
<p>There are currently no items to display.</p>
-
{% endif %}
-
-
{% endblock %}
0 1793 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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>
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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>
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |