Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:02:16+00:00 2026-06-18T07:02:16+00:00

Using Django I’m creating a thumbnail of an image uploaded using a standard ImageField,

  • 0

Using Django I’m creating a thumbnail of an image uploaded using a standard ImageField, adding the path to a table in a separate field ‘thumbnail’ alongside the usual imagefile field. I’m trying to work out how to access the thumbnail path where rendering the imagefield objects in a custom template using a formset so that I can display it.

I guess I need to add a ‘form=CarImageForm’ to inlineformset_factory and then modify my forms.py but I’m having trouble working out how to do it or even whether this approach is the right one. For clarity I have not included my attempts to do so in the code samples below.

My end goal is to return the thumbnail image which links to the original image – already displayed via the Imagefile field.

Thanks in advance!

template is:

<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
    {% for field in form %}
        {{ field.label }}: {{ field }}<br>
    {% endfor %}
{% endfor %}
<p><input type="submit" value="Enter"/></p>

forms.py:

class CarForm(ModelForm):
    class Meta:
        model = Car
        exclude = ['owner', 'uploaded']

views.py:

# Edit an existing record
@login_required
def edit_existing(request, object_id=False):
    try:
        car = Car.objects.get(pk=object_id)
    except Car.DoesNotExist:    
        raise Http404   
    ImageFormSet = inlineformset_factory(Car, CarImage, extra=1, max_num=1)
    if request.method == 'POST':
        form = forms.CarForm(request.POST, instance=car)
        formset = ImageFormSet(request.POST, request.FILES, instance=car)
        if formset.is_valid() and form.is_valid():
            # Handle form.save() to include user id
            new_car = form.save(commit=False)
            new_car.owner = request.user
            new_car.save()
            # Formset - contains the attached images
            formset.save()  
            return HttpResponseRedirect(new_car.get_absolute_url())
    else:
        form = forms.CarForm(instance=car)
        formset = ImageFormSet(instance=car)
    return render_to_response('edit_existing.html',
        {'form': form, 'formset': formset},
        context_instance=RequestContext(request))

models.py:

class Car(models.Model):
    make = models.CharField(max_length=64)
    model = models.CharField(max_length=64)
    owner = models.ForeignKey(User,editable=False)
    uploaded = models.DateField(default=datetime.date.today,editable=False)

    def get_absolute_url(self):
        return reverse('vehicle_admin.views.car_detail', args=[str(self.id)])

def orig_car_id_folder(instance, filename):
    return 'uploads/images/orig/{0}/{1}'.format(instance.car_id, filename)

def thumb_car_id_folder(instance, filename):
    return 'uploads/images/thumb/{0}/{1}'.format(instance.car_id, filename)

class CarImage(models.Model):
    car = models.ForeignKey(Car)
    imagefile = models.ImageField(upload_to=orig_car_id_folder)
    thumbnail = models.ImageField(upload_to=thumb_car_id_folder, editable=False)

    # PIL tips from 
    # https://snipt.net/danfreak/generate-thumbnails-in-django-with-pil/
    # http://www.mechanicalgirl.com/post/image-resizing-file-uploads-doing-it-easy-way/
    def save(self):
        import os
        from PIL import Image
        from cStringIO import StringIO
        from django.core.files.uploadedfile import SimpleUploadedFile
        THUMBNAIL_SIZE = (75, 75)
        super(CarImage, self).save() # Use the commit=False param here?
        image = Image.open(self.imagefile.path)
        if image.mode not in ('L', 'RGB'):
            image = image.convert('RGB')
        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
        temp_handle = StringIO()
        image.save(temp_handle, 'png')
        temp_handle.seek(0)
        name_ext = os.path.splitext(os.path.split(self.imagefile.name)[-1])
        suf = SimpleUploadedFile(name_ext[0],
                temp_handle.read(), content_type='image/png')
        self.thumbnail.save(suf.name+'.png', suf, save=False)
        super(CarImage, self).save()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:02:17+00:00Added an answer on June 18, 2026 at 7:02 am

    I think this is a basic oversight on my part.

    My problem was setting editable=False in my CarImage model for the ‘thumbnail’ field. This is fine for the initial data submission but causes problems where editing the entry as that field is automatically excluded in the form. (See my code snippets above for more info.)

    My next step would be to set up a custom Form for my add_new view (I did not initially include this view but it is what I was using to enable the initial upload of data) like this:

    class InitialCarImageForm(ModelForm):
        class Meta:
            model = CarImage
            exclude = ['thumbnail']
    

    I guess that the right approach is to use editable=False but unless I’m committing a grave mistake taking this approach it will probably work for me.

    I’m new to the site so please let me know if answering my question in this way is the correct way to close it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Django's standard comment system and I would like to extend its anti-spam
Using Django 1.1, how could I create a crosstab (pivot table) SQL query using
Using django-south, is it possible to setup a table only to the last, most
Using Django 1.2, I'm running the django-registration application, and creating a User Profile object
I've using django 1.4. When creating a new user, it saves plain password. Is
I am using django-tinymce v1.5.1b2. I am also using an HTMLField() model field like
Using Django, how do I generate the value of a field the first time
When using Django's built in comment package, where is the table created when the
Using django, which is the way to check if data is present? I know
Using django comments framework http://docs.djangoproject.com/en/dev/ref/contrib/comments/ Not sure is there option, to make all comments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.