Trouble running example django code - python

Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
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.)
What am I doing wrong?

You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py

Related

Django, importing models class into new file. Import error attempted relative import with no known parent

I am working on learning Django by making a simple analysis dashboard. I did the startproject command and the startapp command like the tutorial ran through. I added a new file called connectionOracle.py in the app folder.
My folder structure is (top folder is was created via venv)
AnalysisSite
|AnalysisSite
|coreAnalysis
||models.py
||connectionOracle.py
I have a class called WorkOrder in the models.py file. I am trying to import it into the connectionOracle.py file by doing the following
from .models import WorkOrder
I then go to the Command Line (on windows) and navigate tot he coreAnalysis folder and run the following
python connectionOracle.py
I get an error that says.
ImportError: attempted relative import with no known parent package
I did some reading online, and I tried doing an absolute path with AnalysisSite.AnalysisSite.coreAnalysis.models
that didnt work. I also tried moving the connection file to different directories and that didnt work either. I also tried going into the command line and typing set DJANGO_SETTINGS_MODULE = AnalysisSite.settings
I also put a _init_.py in each folder. (Django automatically put it into the project directory and app directory).
I am not sure what I am doing wrong
you are trying to access Django components(models) by a script file ,
the caller in this case not Django itself ( the request not coming from url or different django tools or mechanism),
anyway in your custom python file which is 'connectionOracle.py'
try to do some steps before accessing the models itself,
the steps are available on the following URL:
https://stackoverflow.com/a/68936419/12662056
############
change the path for project depending on your project path.
i hope this helpful

Django Trouble: django.setup() throws "ImportError: No module named 'project_name'"

I've been having an issue that I believe can be solved one of 2 ways. We have a django project I am trying to write some iPython notebooks for teaching purposes and have them in a tutorials directory that's one below the root of the project (i.e. ~/tutorials).
In the beginning of the notebooks I need to import several modules but I encountered a similar problem to the one detailed in this stack overflow question: Relative imports in Python 3 , (python couldn't find the paths, even though it could when I ran the notebooks from the root directory)
My solution was to specify the absolute path to the root of the directory in a path variable and append it like the following:
import sys
path = 'Users/greg/project_name'
sys.path.append(path)
That worked and I have been able to make the imports. However, the problem I am encountering now is that when I run a function that calls get on a Django model like so:
Platform.objects.get(name='platform_name').platform_levels.get(db_name=level).id
it raises an error from the python3.4/site-packages/django/apps/registry.py file saying:
AppRegistryNotReady: Models aren't loaded yet.
Does anybody have any insight into why they aren't being loaded and what I need to do differently? Please let me know if there's other information that I should provide to help you answer this question. Thanks!
Edit: The suggestion by #albar led to a new error which I think is the root of all the issues. When I go to run django.setup(), it raises an error on this line:
mod = importlib.import_module(self.SETTINGS_MODULE)
saying:
ImportError: No module named 'project_name'
Does it need to be able to import the entire project as a module and if so, how do I tell it how to find it?
You have to setup Django in your script:
import django
django.setup()
EDIT: Before that, you have to define your settings module:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

Django settings.py Error: Import by filename is not supported

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.

Managing multiple settings.py files [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to manage local vs production settings in Django?
I have managed to deploy successfully a Django project on Apache's Web Server with mod_wsgi.
I would like some recommendations on how to manage multiple settings.py files. Right now I have one for development and one totally different for production (regarding DB parameters, static content localization, and stuff like that). My settings.py file is versioned (don't know if this is a good practise) and I deploy it with something like:
$ hg archive myproject.tbz2
$ cd /path/of/apache/web/project/location
$ bzip2 -db /home/myself/myproject/myproject.tbz2 | tar -xvf -
It's working OK. But I find myself manipulating multiple settings.py files.
I guess my question is: what are the best practices when deploying DJANGO PROJECTS regarding multiple settings.py file versions?
I use a settings module that is not a single file:
settings/
__init__.py
_base.py
_servers.py
development.py
production.py
testing.py
The __init__.py file is simple:
from _servers import get_server_type
exec("from %s import *" % get_server_type())
The _base.py file contains all of the common settings across all server types.
The _servers.py file contains a function get_server_type() that uses socket.gethostname() to determine what type of server the current machine is: it returns development, production or testing.
Then the other files look a bit like (production.py):
DEBUG=False
TEMPLATE_DEBUG=False
from _base import *
In each of these files, I put in the settings that only apply to this server type.
The trick that seems to be the most common is to maintain both a settings.py and local_settings.py (one for each environment) file.
Environment agnostic settings go into settings.py and at the bottom of the file, you'll import from local_settings.py
try:
from local_settings import *
except ImportError:
pass
You can override any settings.py settings in the appropriate local_settings.py
django-admin.py / manage.py both accept a --settings=mysite.settings option. In development you could explicitly specify --settings=dev_settings. You can also set the DJANGO_SETTINGS_MODULE environment variable in your apache configuration.
Personally, I simply don't check in settings.py. Instead I check in multiple settings files (dev_settings, prod_settings, etc) and symbolically link them to settings.py as desired. This way if I simply checkout my application it won't be runnable until I think about which settings file is appropriate and actually put that settings file in place.
Another suggestion I've heard but I don't particularly like is having a settings.py that dynamically imports a dev_settings.py if it exists. While this may be more convenient I'd be concerned that it's harder to read settings.py and know what the settings will actually be without also looking for overriding values in a dev_settings.py file that may or may not exist.
My preferred way is to load a separate ini file using ConfigParser, based off a single setting or environment variable. Anyway in the django wiki there are many different options described: http://code.djangoproject.com/wiki/SplitSettings

Google App Engine won't recognize facebook package unless I rename it

I'm trying to intergrate Facebook Connect into an GAE app. I've got a basic folder structure like so:
/gae-root
/myapp
/templates
/etc
app.yaml
settings.py
and I tried to add the PyFacebook library like so:
/gae-root
/myapp
/templates
/etc
/facebook
/djangofb
app.yaml
settings.py
I thought this would work, but now when I try to import facebook it throws a module not found error. What's even weirder is that if I rename the directory from facebook to foo, the import now works but I'll hit errors later when I try to get the current logged in user.
Literally all I did was move the directory into my folder structure and try an import. What am I missing? Sorry if this is an easy question.
It was a problem with an extra .pth file in my site-packages directory.

Categories