startapp with manage.py to create app in another directory - python

My Django project structure is:
/proj
/frontend
/server
/proj
/app1
/app2
manage.py
How do I run python manage.py startapp app_name so that my newly created apps are within the /server directory? I tried running django-admin.py startapp appname within the server directory to create the app but I would end up with this error:
./manage.py runserver
Output:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
commands = get_commands()
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/Users/bli1/Development/Django/CL/cherngloong/cherngloong/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

You can specify the path to ./server/appname directory after appname as the destination, i.e., where the Django app directory structure will be created.
From the startapp documentation:
startapp <app_label> [destination] # startapp command usage
Creates a Django app directory structure for the given app name in the
current directory or the given destination.
If only the app name is given, the app directory will be created in
the current working directory.
If the optional destination is provided, Django will use that existing
directory rather than creating a new one
So, you can specify the path to your ./server/appname directory as the destination value.
django-admin.py startapp appname [destination] # Specify destination
What do you need to do?
1. You need to first create a directory, appname, inside /server.
mkdir ./server/appname # Create a directory from the root level
2. Then, run the startapp command to create the app.
django-admin.py startapp appname ./server/appname

I always have my app in an internal folder (the same that Django creates, with the name of the project) following the design of Two Scoops of Django that is similar to what you want to do. When you want to create a new app, you can use, as the previous answer says,
python ../manage.py startapp my_new_app
from within the folder in which you want to create the app. Another thing, even easier that is what I do, is that you can run
django-admin startapp my_new_app
from this inner folder, of apps and it will work.

If you are already in the server directory, then you can run
python ../manage.py startapp appname
And appname will be created in the server directory instead of in the project root.

To Create a New App in Django project.
First:
Go to your folder using Terminal, where you want to create app
Second:
Type the below command in terminal,
django-admin startapp <new_app_name>
Now, you can check, new app created with the given name.

The simplest way I found is to go inside the server folder and create an __init__.py file:
touch __init__.py
Then, create any app you want:
django-admin startapp {{APP NAME}}

If you started the project using cookiecutter-django, then steps are given in the link below.
The difference from the accepted answer and other answers I see here is that you need to change the name of the app in apps.py as an extra step.
New apps created are located in the project root, not inside the project slug #1725
To copy paste that here:
create the <name-of-the-app> app with python manage.py startapp
move <name-of-the-app> directory to the <project_slug> directory
edit <project_slug>/<name-of-the-app>/apps.py and change name = "<name-of-the-app>" to name = "<project_slug>.<name-of-the-app>"
add "<project_slug>.<name-of-the-app>.apps.<NameOfTheAppConfigClass>", in your LOCAL_APPS in file config/settings/base.py
Here <project_slug> would be your "server" directory.

Use an absolute path for the manage file. It works well for me, and here is an example to run from the destination folder:
python /home/user/project_name/manage.py startupapp app_name

Related

Django - app in subfolder

I've created new django app in a subdirectory using the command:
python manage.py startapp appName subFolder/appName
but if I try to add this app to INSTALLED_APPS at the end of the list I see the following error:
ImportError: No module named appName
Does anyone know what I am doing wrong?
You need to include the subfolder when you add the app to INSTALLED_APPS, for example:
'subFolder.appName',
or
'subfolder.appName.apps.AppNameConfig',
I tried different options but no one can solve that issue.
Finally, I found a solution.
Simply go to the subFolder/appName/app.py file and
replace this line name = 'appname' with name = 'subfolder.appname'
And then you can simply add to the installed apps list.
'subfolder.appname'

Django path to settings file

I am trying to create custom Django template. The structure of the project template is as follows:
project_name/
docs/
reqs/
src/
...
settings/
...
dev.py
prod.py
manage.py
wsgi.py
...
Procfile
requirements.txt
I am having trouble figuring out the right way to include the settings file at manage.py and wsgi.py. Should it be src.settings.dev or settings.dev?
At present,
manage.py has os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev") (at localhost)
and
wsgi.py has os.environ.setdefault("DJANGO_SETTINGS_MODLE", "src.settings.prod"). (at heroku)
Both of which works fine with commands ./manage.py runserver and gunicorn src.wsgi respectively.
I wish to know what determines the path to settings. Is it the file being executed or the project path? Any help is appreciated.
In fact, it has nothing to do with either of these. The settings file is a normal Python module, and so it must be able to be imported like any other Python module - which means that the base of the path must be on the PYTHONPATH.
In your case, something is probably adding both "/path/to/project_name" and "/path/to/project_name/src" to the PYTHONPATH, either by settting the environment variable, or by directly modifying sys.path inside the script.
I have come to know that when settings file is supplied to manage.py, it tries to find the settings at two places,
1. At the path relative to it (manage.py)
2. and at PYTHONPATH

Running Unit Test w/ App Directory In Django 1.6+

I have my Django project structured so all the apps are in the apps directory.
/manage.py
/apps/events/tests.py
/apps/contacts/tests.py
This worked until Django 1.6, but now when I try to run
./manage.py test events
I get the following error:
File "/Users/josephmisiti/mathandpencil/projects/xxxx/lib/python2.7/site-packages/django/test/runner.py", line 63, in build_suite
tests = self.test_loader.loadTestsFromName(label)
File "/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
module = __import__('.'.join(parts_copy))
ImportError: No module named events
I my settings.py, I have the following under INSTALLED_APPS
'apps.contacts',
'apps.events',
Anyone know how to fix this one ?
In 1.6 the command is:
$ ./manage.py test events
The docs assume your folder structure has every app in the root of the project folder.
But if you have structured your folders differently you will need to match that in the command.
So from your project root, if you have a folder named apps and in there an app named events your command will look like:
$ ./manage.py test apps.events

django-admin.py and python path on EC2 Amazon Beanstalk

I deployed my django app on Elastic Benastalk, but my commands are failing and I think that the problem is that django-admin.py is not in the $PYTHONPATH, I would like to add my app to the python path, but I don't know what is the exact path on the EC2 instance.
Actually I found it under: /opt/python/bundle/3/app (I used "find" command via SSH)… but is that a fixed and reliable path??
ps: WTF is that "3"?? (for sure not the version or the count of my deploys ^_^)
UPDATE:
if I cd to /opt/python/bundle/3/app/myappname and run:
python manage.py shell
I get:
File "manage.py", line 3, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
SOLVED!
Amazon Beanstalk uses a virtualenv (like I do myself locally), in order to activate it you have to:
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
python manage.py commandname
BUT, in order to use custom management commands (that is why I need to access to the django shell on my EC2 instance), you have to add your application to the python path and also all the environment variables used by your app, so I did:
vi /home/ec2-user/.bash_profile
and added:
export PYTHONPATH=$PYTHONPATH:/opt/python/current/app
and my env variables... now it works! :)
In order to automatically activate the virtualenv and to be able to use the django shell as soon as logged via ssh, is it possible to add:
source /opt/python/run/venv/bin/activate
cd /opt/python/current/app
in the .bash_profile :)
You can add an option_name if you need to change PYTHONPATH, or set any environment variable in general.
In your .ebextensions/myapp-env.config file (or whatever your *.config is named):
option_settings:
- option_name: PYTHONPATH
value: /opt/python/ondeck/app/myapp:$PYTHONPATH
The message:
File "manage.py", line 3, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
is not because your app is not in the PYTHONPATH but rather because it can't find your django application at all. Meaning your site-packages directory is not in the PYTHONPATH.
Try to find the site-packages directory in your server and that should in the PYTHONPATH. I haven't deployed a python app with Elastic Beanstalk but I believe it maybe using a python virtual environment. In which case you need to source your virtual environment before running python ./manage shell

How do I get all files for my django app into one folder?

I currently have a Django app named reserve. In the folder named "reserve" I have most of the content of my app (views.py, urls.py, models.py, templates folder). However, I have a folder outside of "reserve" named "booking" that has only my settings.py. I tried consolidating by putting the settings.py in "booking" into "reserve" but I seem to be getting an error. Any advice on how to have only one folder with all contents?
The error I get is: ImportError: Could not import settings 'booking.settings' (Is it on sys.path?): No module named booking.settings
overall project folder
booking folder
settings.py
init.py
reserve folder (the app)
views.py
admin.py
models.py
...
what version of django are you using? what command are you running ? assuming that you are just trying to run a command using manage.py you could just chnage manage.py to reflect this if you are going to permanently keep this project structure
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
you should change os.environ.setdefault to the correct directory for your settings.py
I'm assuming you have manage.py at the overal project directory? I'm also assuming you want to just runserver.
It seems you moved the settings.py file outside the overal project directory. If you do this, you'll have to specify the settings file path when you do runserver, or you can also export the django settings module to point to your new settings path (using dot notation). In this case, I think it would be (in the command line):
export DJANGO_SETTINGS_MODULE=<overal-project-folder-name>.booking.settings
python manage.py runserver

Categories