Re: unable to import Storage from django.core.files.storage into settings.py

2008-09-18 Thread Faheem Mitha

On Thu, 18 Sep 2008 12:16:52 -0400, Marty Alchin <[EMAIL PROTECTED]> wrote:
>
> On Thu, Sep 18, 2008 at 11:55 AM, Faheem Mitha <[EMAIL PROTECTED]> wrote:
>> Putting the following into my settings.py (checked for two different
>> projects for Dango 1.0)
>>
>> from django.core.files.storage import Storage
>>
>> gives
>>
>> [EMAIL PROTECTED]:/var/django/hg$ python manage.py validate
>> Error: Can't find the file 'settings.py' in the directory containing
>> 'manage.py'. It appears you've customized things.
>> You'll have to run django-admin.py, passing it your settings module.
>> (If the file settings.py does indeed exist, it's causing an ImportError
>> somehow.)
>>
>> Is there some reason this is not possible, or am I doing something wrong?
>> Can anyone reproduce this? Please cc me on any reply.
>
> Line 5 of django.core.files.storage looks like this:
>
> from django.conf import settings
>
> That means that your settings module imports Storage, which in turn
> imports settings. This is known as a circular import, which is never a
> good thing. So, yes, you're doing something wrong, because that
> shouldn't work.
>
> Now, the real question is: why are you trying to import Storage in
> your settings.py? What is it you're trying to accomplish? You're
> certainly using the wrong approach at the moment, but I can't
> recommend anything else without knowing what you're trying to do.

I was trying to do something like (in settings.py)

***
from customstorage import FileSystemStorage

DEFAULT_FILE_STORAGE = FileSystemStorage
***

where customstorage.py looks as below. This was failing (I think)
because the following line in customstorage.py failed to be imported
into settings.py

from django.core.files.storage import FileSystemStorage

I then belatedly realised that that the settings are strings, and did

DEFAULT_FILE_STORAGE = "customstorage.FileUploadStorage"

instead, which works.

Thanks for the reply and clarification, and sorry for the noise. If
there is anything I missed, please feel free to educate me further.

Thanks, Faheem.

***
customstorage.py
***
import os
from django.core.files.storage import FileSystemStorage
from django.utils._os import safe_join
from django.conf import settings

class FileUploadStorage(FileSystemStorage):
def path(self, name):
print "calling FileUploadStorage class."
try:
path = safe_join(settings.MEDIA_ROOT, name)
except ValueError:
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
return os.path.normpath(path)

**


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: unable to import Storage from django.core.files.storage into settings.py

2008-09-18 Thread Marty Alchin

On Thu, Sep 18, 2008 at 11:55 AM, Faheem Mitha <[EMAIL PROTECTED]> wrote:
> Putting the following into my settings.py (checked for two different
> projects for Dango 1.0)
>
> from django.core.files.storage import Storage
>
> gives
>
> [EMAIL PROTECTED]:/var/django/hg$ python manage.py validate
> Error: Can't find the file 'settings.py' in the directory containing
> 'manage.py'. It appears you've customized things.
> You'll have to run django-admin.py, passing it your settings module.
> (If the file settings.py does indeed exist, it's causing an ImportError
> somehow.)
>
> Is there some reason this is not possible, or am I doing something wrong?
> Can anyone reproduce this? Please cc me on any reply.

Line 5 of django.core.files.storage looks like this:

from django.conf import settings

That means that your settings module imports Storage, which in turn
imports settings. This is known as a circular import, which is never a
good thing. So, yes, you're doing something wrong, because that
shouldn't work.

Now, the real question is: why are you trying to import Storage in
your settings.py? What is it you're trying to accomplish? You're
certainly using the wrong approach at the moment, but I can't
recommend anything else without knowing what you're trying to do.

-Gul

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---