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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:25:54+00:00 2026-05-12T11:25:54+00:00

Ok, I’ve tried about near everything and I cannot get this to work. I

  • 0

Ok, I’ve tried about near everything and I cannot get this to work.

  • I have a Django model with an ImageField on it
  • I have code that downloads an image via HTTP (tested and works)
  • The image is saved directly into the ‘upload_to’ folder (the upload_to being the one that is set on the ImageField)
  • All I need to do is associate the already existing image file path with the ImageField

I’ve written this code about 6 different ways.

The problem I’m running into is all of the code that I’m writing results in the following behavior:
(1) Django will make a 2nd file, (2) rename the new file, adding an _ to the end of the file name, then (3) not transfer any of the data over leaving it basically an empty re-named file. What’s left in the ‘upload_to’ path is 2 files, one that is the actual image, and one that is the name of the image,but is empty, and of course the ImageField path is set to the empty file that Django try to create.

In case that was unclear, I’ll try to illustrate:

## Image generation code runs.... 
/Upload
     generated_image.jpg     4kb

## Attempt to set the ImageField path...
/Upload
     generated_image.jpg     4kb
     generated_image_.jpg    0kb

ImageField.Path = /Upload/generated_image_.jpg

How can I do this without having Django try to re-store the file? What I’d really like is something to this effect…

model.ImageField.path = generated_image_path

…but of course that doesn’t work.

And yes I’ve gone through the other questions here like this one as well as the django doc on File

UPDATE
After further testing, it only does this behavior when running under Apache on Windows Server. While running under the ‘runserver’ on XP it does not execute this behavior.

I am stumped.

Here is the code which runs successfully on XP…

f = open(thumb_path, 'r')
model.thumbnail = File(f)
model.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-05-12T11:25:54+00:00Added an answer on May 12, 2026 at 11:25 am

    I have some code that fetches an image off the web and stores it in a model. The important bits are:

    from django.core.files import File  # you need this somewhere
    import urllib
    
    
    # The following actually resides in a method of my model
    
    result = urllib.urlretrieve(image_url) # image_url is a URL to an image
    
    # self.photo is the ImageField
    self.photo.save(
        os.path.basename(self.url),
        File(open(result[0], 'rb'))
        )
    
    self.save()
    

    That’s a bit confusing because it’s pulled out of my model and a bit out of context, but the important parts are:

    • The image pulled from the web is not stored in the upload_to folder, it is instead stored as a tempfile by urllib.urlretrieve() and later discarded.
    • The ImageField.save() method takes a filename (the os.path.basename bit) and a django.core.files.File object.

    Let me know if you have questions or need clarification.

    Edit: for the sake of clarity, here is the model (minus any required import statements):

    class CachedImage(models.Model):
        url = models.CharField(max_length=255, unique=True)
        photo = models.ImageField(upload_to=photo_path, blank=True)
    
        def cache(self):
            """Store image locally if we have a URL"""
    
            if self.url and not self.photo:
                result = urllib.urlretrieve(self.url)
                self.photo.save(
                        os.path.basename(self.url),
                        File(open(result[0], 'rb'))
                        )
                self.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.