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 7035501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:18:42+00:00 2026-05-28T01:18:42+00:00

I am new to web development, and I am working on a basic image

  • 0

I am new to web development, and I am working on a basic image gallery app (learning exercise) using Django. I have it set up so I can upload a zip full of images all at once to create a new album. This all seems to work OK, but I am getting an HTTP 504 error when the uploaded file is especially large.

I gather (please correct me if I am wrong) this error means my app is too slow to return an HTTP response. I am guessing this is because it takes a long time to unzip and process (create a Pic object in the DB and create thumbnails) all the images.

Is there a way to return a response (say to some intermediate page) while still performing the processing in the background – maybe using threads? What is the proper way to handle this? Is it time for me to start learning Javascript/AJAX?

Thank you!


Models:

from django.db import models
from blog.models import Post

class Album(models.Model):
    title = models.CharField(max_length=128)
    slug = models.SlugField()
    description = models.TextField()
    parent = models.ForeignKey('self', null=True, blank=True)

    pub = models.BooleanField()
    date_created = models.DateTimeField(auto_now_add=True)
    date_published = models.DateTimeField(null=True, blank=True)
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.title

class Pic(models.Model):
    image = models.ImageField(upload_to='pics/%Y/%m') 
    title = models.CharField(max_length=128)
    caption = models.TextField(blank=True, null=True)
    albums = models.ManyToManyField('Album', null=True, blank=True)
    posts = models.ManyToManyField(Post, blank=True, null=True)

    date_taken = models.DateTimeField(null=True, blank=True) 
    date_uploaded = models.DateTimeField(auto_now_add=True) 
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.title

View:

I’m doing this manually because I didn’t grok the Django admin when I started. I think it might be better to use admin customization here.

def new_album(request):
    if request.method == "POST":
        form = AlbumForm(request.POST, request.FILES)
        if form.is_valid():
            from gallery.pic_handlers import handle_uploaded_album
            pics = handle_uploaded_album(request.FILES['pic_archive'])
            a = form.save()
            a.slug = slugify(a.title)
            a.save()
            for pic in pics:
                pic.albums.add(a)
            return HttpResponseRedirect('/gallery/album/%s/' % a.slug)
    else:
        form = AlbumForm()

    return render_to_response('new_album.html', {
        'form' : form,
    }, context_instance = RequestContext(request))

Additional processing:

def handle_uploaded_album(pic_archive):
    destination = open(join(settings.MEDIA_ROOT,pic_archive.name), 'wb+')
    for chunk in pic_archive.chunks():
        destination.write(chunk)
    destination.close()

    today = datetime.date.today()
    save_path = 'pics/{0}/{1:02}/'.format(today.year, today.month)
    tmp_path = 'tmp/'
    z = zipfile.ZipFile(join(settings.MEDIA_ROOT,pic_archive.name), 'r')
    pics = []
    for member in z.namelist():
        if '/' in member or '\\' in member: 
            # don't deal with any directories inside the zip
            # this also solves the '__MACOSX' issue
            continue
        if splitext(member)[1] in IMG_EXT:
            z.extract(member,join(settings.MEDIA_ROOT,tmp_path))
            im = File(open(join(settings.MEDIA_ROOT,tmp_path,member), 'rb'))
            # create a Pic from this file
            pic = Pic()
            pic.title = member
            pic.image.save(
                join(save_path, member),
                im,
                True)
            create_thumbnails(pic)
            im.close()
            # remove extracted images
            remove(join(settings.MEDIA_ROOT,tmp_path,member))

            # TODO: save date taken if available
            pics.append(pic)

    z.close()
    remove(join(settings.MEDIA_ROOT,pic_archive.name))

    return pics

def create_thumbnails(pic):
    fname, ext = splitext(pic.image.path)
    img = Image.open(pic.image.path)

    img.thumbnail((512,512), Image.ANTIALIAS)
    img.save(fname + '_m' + ext)

    img.thumbnail((128,128), Image.ANTIALIAS)
    img.save(fname + '_s' + ext)
  • 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-05-28T01:18:42+00:00Added an answer on May 28, 2026 at 1:18 am

    Long tasks such as this processing take too much time, and your client and/or your proxy will timeout – which is the 504 error you see.

    you should not run long jobs this way!

    As you correctly ask at the end, you need a way to detach long executions – via an asynchronous queue system, such as celery. This way you can return an answer straight away to your clients, while the backend runs jobs asynchronously.

    you should look at one of the followings:

    • Django long running asynchronous tasks with threads/processing
    • Long running tasks with Django
    • Long polling in Django

    Since django-celery is definitely the best option, next step is to learn about it; there are also a lot of SO questions around it

    If you want to be sure it’s processing that fails, and not upload, simply try disabling all processing and returning straight away to your client. If it’s still failing, you also need to tweak your webserver so that it’s not going in timeout!

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

Sidebar

Related Questions

I am relatively new to web development and learning all the time. I have
We are working on a new web site using Apache, Python and Django. In
I'm new to web development and working on my first ASP.NET MVC 3 app.
I'm pretty new to web development. Lately I have been making a site using
I am new to web development, I have to create a web application in
I am quite new to web development and have a task to develop a
I'm working on my first asp.net/web development application. Obviously, I'm very new to asp.net
I am a web-developer working in PHP. I have some limited experience with using
So I am very new to web development and working on a new project
I have methodically been working thru the Agile Web Development with Rails book. No

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.