I've a setting say gi in settings.py file. I've created a separate python file (i.e. I'm not using it in views) and used import statement as:
from django.conf import settings
but when I try to access settings.gi, it says that 'Settings' object has no attribute 'gi'. What's missing? :s
From the Django docs on creating your own settings states:
Setting names are in all uppercase.
Try renaming the setting to GI.
The project must be added on the PYTHONPATH and the DJANGO_SETTINGS_MODULE has to be defined.
Please refer to the commands mentioned below.
export PYTHONPATH=PATH_OF_PROJECT
export DJANGO_SETTINGS_MODULE=settings
Related
I'm using django and need to decouple setting data from source code,
because setting information must be hidden.
so tried python-decouple module.
I tried .ini and .env file both.
when using setting.ini file, I located it next to setting.py(same directory)
when using setting.env, located setting.py's parent derictory.
both occur error like this.
SECREAT_KEY not found. Declare it as envvar or define a default value.
setting.ini file
[settings]
SECRET_KEY=1234
setting.env file
SECRET_KEY=1234
source code in setting.py
from decouple import config
SECRET_KEY = config('SECRET_KEY')
I already installed python-decouple
pip install python-decouple
how can I fix it?
please help me
os = window
I figured it out.
reson was .ini file's name.
file name must be settings.ini and my file name was setting.ini
To be honest I don't see the need for a special module for this. You could just as well create a py file next to the settings.py file (I tend to name it something like local_settins.py), add your settings and secrets to that file, then just add from local_settings import * to settings.py. But I guess everyone has their own preference. :)
I'm documenting a django project of mine but got stuck with some errors when I run make html.
If I go to the .py file where the error is happening and comment the imports at the beginning of the file, the make html command runs flawlessly.
And this is part of the error message:
ImproperlyConfigured: Requested setting CACHES, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.
I tried this Sphinx Docs not importing Django project settings but nothing.
I found on this on this website(link), and worked for me.
I had to include it in my conf.py file.
sys.path.insert(0, os.path.abspath('..'))
from django.conf import settings
settings.configure()
How can I permanently set the environmental variable DJANGO_SETTINGS_MODULE on WINDOWS on a permanent basis and be done with it?
I mean
Win Button + Pause/Break Button
This leads to Control Panel\System and Security\System
Click Advanced System Settings
Click Environment Variables
There are two boxes the first is titled User variables and the second System variables
On the System variables click the New Button
For variable name put in DJANGO_IMPORT_SETTINGS
XXX--> WHAT DO I PUT IN VARIABLE VALUE TO SET IT ONCE AND FOR ALL?
In the Django Site on this issue it states:
DJANGO_SETTINGS_MODULE
When you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax,e.g. mysite.settings. Note that the settings module should be on the Python import search path.
What does it mean ...should be in Python path syntax e.g. mysite.settings... ?
I have a certain directory where my Python is located:
C:\Python27
I have a certain directory where my Django is located: C:\Python27\Lib\site-packages\django
What does this mysite means. What directory is it meanning C:\Something......
Can you put this variable once and for all or you have to constantly change it for every project (I hope not!)
And what does this suspiciously line means Note that the settings module should be on the Python import search path.
All I want it to set the DJANGO_SETTINGS_MODULE environmental variable and be done once and for all from this hassle
EDIT
In order to work, Django just has to be pointed at a valid settings file, and by default it
looks for an environment variable named DJANGO_SETTINGS_MODULE to tell it where to find the
settings. The value of this variable should be the Python import path of the settings file, such
as cms.settings.
--> What king of directory is this: cms.settings? In windows every directory starts with a hard drive as C:\Something...... How can you start a directory like this in Windows?
EDIT_2
Excerpt from a book
PROBLEM
Environment variable DJANGO_SETTINGS_MODULE is undefined.
SOLUTION
Run the command python manage.py shell rather than python.
MY QUESTION --> ON WHAT DIRECTORY?///CAN YOU SET IT FOR ONCE OR IS IT DIFFERENT PER PROJECT?
MY PROJECT IS STRUCTURED LIKE THIS
C:\Python27\pysec-master(file)
|__local_settings.py
|__manage.py
|__settings.py
|__C:\Python27\pysec(file)
|__ __init__.py
|__example.py
|__models.py
|__xbrl.py
|__xbrl_fundamentals.py
I am trying to run models.py and I have a settings.py in the C:\Python27\pysec-master
You can find an exact copy here.
MAYBE_IMPORTANT_EDIT
I have a file called manage.py in my project which has these contents
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Does this has to do anything on setting the variable? Do I need to set here here inside the loop?
EDIT
For the command in the IDLE from django.db import settings do i need to set a directory for the PYTHON_MODULE_SETTINGS like C:\Python27\Lib\site-packages\django\db ?
Okay, don't be so frustrated. Let's look at this step by step:
Python path syntax:
In Python, when you split your code base across modules, you qualify the name of the import with the name of the module. Let's say your project is structured like this:
my_project
|__utils
| |____init__.py
| |__file_utils.py
|__my_module
|____init__.py
|__main.py
In your main.py if you want to access methods you have defined in file_utils.py you add an import statement in your main.py like this:
import utils.file_utils.read_file
assuming read_file is the method you want to import into main.py. This way of importing modules where you have a . separating every module is referred as python path syntax.
PYTHONPATH:
In the above example, the import statement would work only if the Python interpreter knows where to look for the first module namely the utils. Only when it finds utils can it find file_utils and read_file. You specify the list of all the paths you want the interpreter to look into in the environment variable PYTHONPATH. So in order to have an import statement like above in your code, you have to make sure that the full path to your project my_project is in PYTHONPATH. Assuming my_project is in C:\AMAZEBALLS_CODE\my_project you should have C:\AMAZEBALLS_CODE in your PYTHONPATH
DJANGO_SETTINGS_MODULE:
Now let's suppose your my_project is actually a Django application. A Django application needs a settings file where you specify a whole bunch of things. In order to instruct Django which settings file to look into you specify it in DJANGO_SETTINGS_MODULE. Assuming this is your project structure:
my_project
|__utils/
| |____init__.py
| |__file_utils.py
|__my_module/
| |____init__.py
| |__main.py
|__site_settings/
|__dev_settings.py
|__production_settings.py
myroject.site_settings is the module Django has to look into for the settings file. And myroject.site_settings.dev_settings is the value you need to be setting to the DJANGO_SETTINGS_MODULE in the above case. When documentation says cms.settings or mysite.settings they mean cms or mysite is the name of your project and settings.py is the name of your settings file.
Now let's look at your question:
Can you permanently set it in the environment variables of Windows? Sure.
Is it the right way? No.
Because if you want to create another application tomorrow in another location, you will have to edit this in the environment variables section of Windows. Also, it is a practice to use a different settings file for development environment and another one for production. So setting it at one place with one value makes it inflexible. But if you are aware of all of the above and sure you are gonna be using just the one settings file set DJANGO_SETTINGS_MODULE to myproject.site_settings.dev_settings in the env variables section.
Hope this helps!
EDIT:
Looks like you are putting your pysec-master project in C:\Python27. Do not put your projects in the python installation. Create a settings.py file in your project and set DJANGO_SETTINGS_MODULE to pysec-master.settings
You said:
All i want it to set the DJANGO_SETTINGS_MODULE environmental variable
and be done once and for all from this hassle
If you don't want to go through a tedious procedure every time, you don't have to make it permanent, you only need to make the procedure automatic.
Create a bat and put set DJANGO_SETTINGS_MODULE='xxx.settings' inside.
If you are using virtualenv, you can set the environment variable in bin/activate.bat.
Following on from my last question Error: No module named psycopg2.extensions, I have updated my mac OS to Mountain Lion and installed Xcode. I have also installed psycopg2 using 'sudo port install py27-psycopg2'. I am now trying to run 'python manage.py runserver' but am receiving this error
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'
Any help on how to fix this and get my localhost running?
From django docs:
A Django settings file contains all the configuration of your Django installation.
When you use Django, you have to tell it which settings you're using.
Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax,
e.g. mysite.settings. Note that the settings module should be on the
Python import search path.
And
ROOT_URLCONF
Default: Not defined
A string representing the full Python import path to your root
URLconf. For example: "mydjangoapps.urls". Can be overridden on a
per-request basis by setting the attribute urlconf on the incoming
HttpRequest object. See How Django processes a request for details.
If you are using multiple settings files, be sure to import the base settings in all the other settings files. This was how I got this error.
If you're working on a Django project, the root of your project(ROOT_URLCONF= variable) isn't defined in your settings.py file, or at least it isn't defined properly.
If you don't have a settings.py file, this block of code should fix the issue:
from django.conf import settings
settings.configure(
# ...
ROOT_URLCONF=__name__,
# ...
),
)
What that does is specify the root of your project runserver knows where to find your project.
If do have a settings.py file, then find that variable and add __name__ to it.
I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.