On Sunday, February 13, 2011 02:37:07 pm vivek_12315 wrote:
> Hello all,
> 
> while coding for views in django, i feel the need of generating files,
> more like logs. But i want all those logs to be generated in a
> specific directory.
> 
> So, can I use my settings.py file for defining such a location of dir.
> and in views files' i can refer to that settings value some how ?
> 

yes.[1]

> Is there a way for this ? Does settings.py allow only standard
> settings to be defined ?
in settings.py add something like this:

MY_LOG_PATH = os.path.join(os.path.abspath(__file__), "mylogs")



in your view.py add this:

from django.conf import settings

# access log path.
print(settings.MY_LOG_PATH)



You can also do per app settings, go around github and check out examples from 
existing projects. but here is the jist of it.

in a settings.py file that's in a app dir:

from django.conf import settings

def get(key, default):
        return getattr(settings, key, default)


MYAPPPATH = get("MYAPPPATH", os.path.abspath(__file__)



This allows you to keep a setting in your app and override that setting with 
the project settings. The caveat here is that these settings aren't available 
through django.conf.settings if they aren't listed in the project settings.py 
file. You have to import the app settings file specifically. The get() acts as 
a middleman between your local app settings and the ones in the project 
settings file By calling getattr() on the main settings it can retrieve the 
setting attr or use the default if the settings attr isn't defined in the 
project settings.

example usage:

from myapp import settings as myapp_settings

# if MYAPPPATH is defined in the project settings.py file, 
# it'll get that value if not use the default in the apps setting file.

print(settings.MYAPPPATH)


If you're still stuck on how this works, the missing pieces are in python 
itself and how it treats each .py as an importable module. [2]

Mike


[1] http://docs.djangoproject.com/en/dev/topics/settings/#using-settings-in-
python-code

[2] http://docs.python.org/tutorial/modules.html
-- 
Time goes, you say?
Ah no!
Time stays, *we* go.
                -- Austin Dobson

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to