When should you use django-admin.py versus manage.py? - django

Background:
When I run the django-admin.py loaddata example.json I get this error. "ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined." I understand the problem. It needs the DJANGO_SETTINGS_MODULE to be able to access the database to do this import. I've had this problem before and I've managed to side step it thus far.
In reading the docs, I discovered that the manage.py is a wrapper for django-admin.py; it puts the project on the sys.path and sets the DJANGO_SETTINGS_MODULE environment. Woot! Whoa! I know how to fix my problem.
Soo...
Why do the Django documentation code examples use django-admin.py instead of manage.py when demonstrating subcommands such as loaddata and dumpdata?

If your DJANGO_SETTINGS_MODULE environment variable is set, you can use django-admin.py from any working directory, whereas you need to be in the project directory to use ./manage.py (or have it in your path).
Use virtualenv, and have DJANGO_SETTINGS_MODULE set by bin/activate, and then you can use django-admin.py.

Why do the Django documentation code examples using django-admin.py instead of manage.py when demonstrating subcommands such as loaddata and dumpdata?
Well, because these scripts are the same in priciple, with the differences, you already mentioned. The Django docs also mention
django-admin.py <subcommand> [options]
manage.py <subcommand> [options]
side by side. Usually you use django-admin.py to start a new project or application and manage.py to do the rest.

Related

Python manage.py startapp does not work

When I run the python manage.py startapp "app name" command in my Django project directory nothing happens... Every other manage.py command works without problem.
What could be the problem?
Thanks a lot!!
Andrew
The first line in manage.py breaks the file on windows. The first line should look like this:
#!/usr/bin/env python
Removing it fixes the issue for me. Please try this is really helpful. Thanks
This problem occurs due to Python Path is not set, you can reinstall python and check the box that says "Add to Path 3.9"
Are you running the correct version of Python and correct version of Django?
To check Django:
python manage.py --version
Make sure you are running the right versions, if you are have Python 3 and Python 2 installed you may have multiple versions of Django.
Try to set environment variables on the command line? This path is if you were using vagrant.
PYTHONPATH=/vagrant/www DJANGO_SETTINGS_MODULE=settings django-admin.py startapp
Then try manage.py does it work? Without setting envs on the command line?
Adding a virtual environment is necessary to get the above command working. In the virtual environment you can specifically install Django and it is done so as to ensure that no other package interfers with the Django framework. I was also facing the same issue but after creating a virtual environment, things became super easy!!!
I would recommend using pycharm as creating a virtual environment is just as easy as running a hello world program!!. Also paste the following code at the settings.py script at the end :
ALLOWED_HOSTS=['*']
Also specify the correct directory while running in the terminal. Be sure to check that the code should be written inside the project directory.
Hope that helps :)
I was facing a similar issue and saw this question. Tried the methods mentioned by some of the users but it did not work. Surprisingly the only thing I had to do was remove the word "python" from the command. All I typed in the command line was - manage.py startapp "appname"
I have no idea what difference does it make or how I made it work, but if anyone has a similar problem I hope your issue gets resolved.
I think it maybe because i had previously added the directory I am working in the PATH variable. Not sure. Someone has an opinion on this, do share.
I think i may have found the solution. If you're running on python 3.7 or above, try setting your directory to ~\YourProjectName\YourProjectName with the cd commmand then type py -m django startapp <appName>
The command
$ python-admin startapp main
returns the error " bash: python-admin: command not found".
I was able to resolve using the following command instead
$ py -m django startapp main
if some of you are following old tutorials on django then after you have python version 3.7 or above then the command "python manage.py startapp 'appname'" doesnt works.//well it didn't work for me so insted of that command you can use "py -m django startapp 'appname'"
it worked for me so you can also try that..
the command -> manage.py startapp 'appname'" works for python version upto 3.7 and for 3.7 and above version used command -> "py -m django startapp 'appname'" it works for me...

How to set up a Django project under windows?

I'm trying to set up a django project but I fail.
First of all I have a Windows 8 machine.
My PATH variable is that:
C:\Python34\;C:\Python34\Scripts;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;-vm
C:\Program Files (x86)\Java\jre7\bin\javaw.exe
When I open PyCharm for example and type import django there comes no error message
Then, in the officialy tutorial "Writing your first Django app" they say I have to create a project with this command django-admin.py startproject mysite
I tried this via command prompt with administrative privileges under Windows from these folders:
C:\Python34\Scripts
&
C:\Python34\Lib\site-packages\django\bin
In both folders I can find a django-admin.py.
There comes no error message, it just rushes through and is not creating any folders. What am I missing here?
The output of the command prompt is this:
C:\Python34\Lib\site-packages\django\bin>django-admin.py startproject myApp
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on exception
--no-color Don't colorize the command output.
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
runserver
shell
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlinitialdata
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
Note that only Django core commands are listed as settings are not properly conf
igured (error: Requested setting INSTALLED_APPS, but settings are not configured
. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.).

run manage.py where manage.py is not present?

manage.py is under project_home/mysite/mysite/bin/manage.py
I did it following a recommendation from some book.
The point is it's rather hard to execute python manage.py something as it stands since I have to move to the directory or type the directory path leading to it.
Is there a convenient way to call manage.py in this setup?
If you want to call manage.py anywhere, you can use Django-manage.py-anywhere. As package description says you can run manage.py commands from any directory of your Django project.
See more here: https://github.com/timonweb/Django-manage.py-anywhere

django-admin.py startproject mysite command not working

I am trying to create very first project using django but stuck at very first step itself. Whenever I give django-admin.py startproject mysite command not working command, nothing happens and help related information comes up on command prompt. I have set PYTHON_HOME, PATH variables set correctly. I am using python 2.7.
My command prompt looks as follows :
C:\Shekhar\djangoWorld>c:\Python27\Scripts\django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
cleanup
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
reset
runfcgi
Why django is not creating project for me?
Please help !!
Add 'python' before django-admin.py (or add full path to python.exe).
C:\Shekhar\djangoWorld>python c:\Python27\Scripts\django-admin.py startproject mysite
Your Django installation is broken. Reinstall.
In my case I'm using Canopy on Windows 8 and the only way it worked for me was putting python command also the full path of django-admin.py like this example "c:> python C:\Users\\AppData\Local\Enthought\Canopy\User\Scripts\django-admin.py startproject ".
I have Canopy installed as well. I copied the django-admin file into the directory where I wanted to install my first project. I opened up the python command prompt and typed in:
python django-admin.py startproject mysite
That did the trick for me. Thanks to those who posted because that got me going down the right path.

manage.py runserver - ImportError: No module named MYSITE

I can't get the dev environment running!
I've been following the instructions here (tutorial on DjangoProject page). When I type
manage.py runserver
I get the following error:
ImportError: No module named MYSITE
I am executing the command within the folder MYSITE which has the files init.py, manage.py, settings.py, urls.py
I've searched around and found questions on "manage.py runserver", but not this specific error. Your help is greatly appreciated. Thanks
If you're using windows, you'll need to put your project's directory (the one with manage.py in it) into an environment variable called PYTHONPATH.
Since this is highly ranked on Google and I got here while searching for an answer I'll share my solution:
I have a project built on my machine, packaged into a debian package and installed on a test machine. On my machine I use the runserver but on the testmachine I use apache (which was using fine). When changing database from sqlite to postgresql I could not do shell or syncdb etc (all manage commands). The problem here was that manage.py was owned by www-data and it had to be root for these commands to work...
sudo chown root. manage.py
Another different answer ;) I'm walking the "Django for the Impatient: Building a Blog" chapter from "Python Web Development with Django" and it suggests creating a "dotted" module so I registered my application as "x.y" which it didn't like. Changing to "y" which matched the file-system as:
/x
+ manage.py
+ /x
+ settings.py
+ /y
+ models.py
Worked for me.