Saher Ahwal
Saher Ahwal

Reputation: 9237

Django image upload succeeds with url written to DB but no image shows upload target folder

I have form in Django

class BusinessForm(forms.Form):
    name = forms.CharField( required = True,
                             max_length=MAX_NAME_LENGTH,                                
                             widget=forms.TextInput(attrs={'placeholder': 'e.g Boston Market'}))        
    image = forms.ImageField()

which belongs to this model

class Business(models.Model):
    """ Describes Bussines data type """    
    name = models.CharField(max_length=BUS_NAME_LENGTH)         

    img = models.ImageField( upload_to = UPLOAD_TO )

where UPLOAD_TO = 'business/images'

and in the settings.py I have media root defined as follows

# Media Root - uploaded images
MEDIA_ROOT = '/uploads/'

in the database after I do an upload through the form I see the urls for example the following

urls shown in mysql database for query

However in the filesystem I do not see the file, I am running in test environment on localhost. I am using Windows OS.

empty folder - explorer

Why is that? Do I have to set some permissions to the folder? If so, how do I also make sure this will work in production when I deploy ? Thanks.

Upvotes: 0

Views: 908

Answers (1)

xyres
xyres

Reputation: 21754

This is happening because MEDIA_URL and MEDIA_ROOT aren't configured properly. Put the following code in your settings.py

MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, "uploads")

Note

If BASE_DIR is not defined in your settings file, put the following code on top of the file:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Upvotes: 2

Related Questions