I'm learning something about Django.In some tutorials that i've read is used django-admin.py for execute commands like " syncdb, startapp and startproject", in the others tutorials is used manage.py, So exist any important diference between use django-admin.py or manage.py ?
See: django-admin.py and manage.py in Django documentation
manage.py is a wrapper around django-admin.py automatically created in each Django project which puts your project's package on sys.path and sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project's settings.py file.
So, I think is more comfortable to use manage.py when you're working on a Django project.
manage.py is a file that excists in a project environment and is created after starting a project. It's purpose is to control project commands. Django-admin.py is a script to start a project ( you can put it anyhwhere, or shortcut it via .bash or .profile )
manage.py is django-admin fitted in your project: it uses settings.py in your project dir
Related
I'm using Django1.11.5 and I created makemessages.py file in "my-app/management/commands/" directory to customise makemessages command.
And I made it to execute this command by running "python ../manage.py makemessages" from my-app directory.
But I want to execute by "django-admin makemessages -l ja".
(Running "django-admin makemessages -l ja" just executes default makemessages command)
Is there any way to execute this customised command by running "django-admin makemessages -l ja"?
I believe it should work if you did all right. Take a look at this docs part:
In addition, manage.py is automatically created in each Django
project. manage.py does the same thing as django-admin but takes care
of a few things for you:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your
project’s settings.py file.
Carefully check this two moments. Since your manage.py works as expected, you already added your app in INSTALLED_APPS (after that Django can find and override default management command).
How to run Django-simple-blog? I tried to install django-simple-blog but could not find the manage.py file to run it. Can I get a solution or another simple blog?
Django has a concept of apps and a concept of projects. A project will have a manage.py file like you mention, and will also have a settings.py file that declares all of the apps that the project uses.
django-simple-blog is an app, meaning you install it within an existing project. After this explaination, the rest of the steps found here should be easier to follow: https://github.com/drager/django-simple-blog/blob/master/README.rst
The remaining steps are to:
Add 'simpleblog' to INSTALLED_APPS in your settings.py file
run the command python manage.py migrate from your project root
include 'simpleblog.urls' into any of your urls.py file
I was trying the official tutorial of Django because I want to learn it. I run Archlinux 4.10.11-1 64 bits.
First, I created a folder named djangoapp where I set my virtual environment:
$ virtualenv djangoapp
I set my current directory to this folder, and then I activated it:
$ source bin/activate
And I installed Django after:
$ pip install django
Following the tutorial, I ran:
$ django-admin startproject djangoapp
And set my current directory to djangoapp, and ran:
$ python manage.py runserver
But I'm getting the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Indeed, running env | grep DJANGO_SETTINGS_MODULE gets me the following result:
DJANGO_SETTINGS_MODULE=
So I tried to set it manually:
$ export DJANGO_SETTINGS_MODULE='djangoapp.settings'
Running $ python manage.py runserver now works, but $ django-admin help now gets me the following error:
ModuleNotFoundError: No module named 'djangoapp'
What did I do wrong? Thanks for your help!
PS: $ python --version gets me Python 3.6.1.
I think the docs could be a little clearer about this, but django-admin is typically only used for running django-admin startproject and manage.py is used for the rest e.g. ./manage.py runserver, ./manage.py migrate etc.
Deleting the DJANGO_SETTINGS_MODULE environment variable should allow you to run both ./manage.py * and django-admin startproject commands when inside the djangoapp folder.
I'm not sure how to do this on Archlinux, but something like unset DJANGO_SETTINGS_MODULE or set -e DJANGO_SETTINGS_MODULE should work.
Further info: you can think of manage.py as a wrapper around django-admin that automatically sets the PYTHONPATH and DJANGO_SETTINGS_MODULE environment variables. Therefore, once inside the project, you can just use ./manage.py rather than django-admin to run the management commands.
I am learning Django from the official documentation and while going through the tutorial at https://docs.djangoproject.com/en/1.7/intro/tutorial01/, I am stuck at creating a project part.
When I run django-admin.py startproject mysite I am getting following error
C:\Python34>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 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 configured
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.).
I am using Python 3.4.1 and django 1.7. I don't have any other Django version installed and this is the first project I am creating.
You can just run django-admin startproject mysite(Note: not django-admin.py), because if you install django by pip, a executable program named 'django-admin.exe' will be added to 'C:\Python34\Scripts', which is already in your environment variable 'PATH' normally(if not, add it to PATH).
I was facing the same issue while installing Django 2.0.5. This can be resolved using Virtual Environments.
Environment details:
Python version: 3.6
OS: Ubuntu 18.xx.x
Steps:
Install Virtual Environment
pip install virtualenv
Create a Virtual work-space('VEnv')
virtualenv --python=python3 VEnv
Activate the Virtual Environment:
cd VEnv/
source bin/activate
Install Django(Version - 2.0.5)
pip install django==2.0.5
Create Project (Project Name: HelloDot)
django-admin startproject HelloDot
Run the server as below and then access it from "http://127.0.0.1:8000/"
cd HelloDot/
python manage.py runserver 8000
For more details, take a look at this: https://www.howtoforge.com/tutorial/how-to-install-django-on-ubuntu/
Make sure that you follow the troubleshooting guide because it looks like you don't have django-admin.py on your system path correctly. From the docs:
django-admin.py should be on your system path if you installed Django
via python setup.py. If it’s not on your path, you can find it in
site-packages/django/bin, where site-packages is a directory within
your Python installation. Consider symlinking to django-admin.py from
some place on your path, such as /usr/local/bin.
You should also use a virtualenv for each of your projects to allow isolation of dependencies per project and easier management of them. virtualenvwrapper is a useful tool for creating and managing your virtualenvs.
When I initially created my Django project, I had this file folder structure:
project/
project/
manage.py
settings.py
...
In the topmost folder I have my git repo initialized as well as my vitual environment.
I want to collapse the top folder level (as if I had run `django-admin.py startproject .':
project/
manage.py
settings.py
However, when I tried to do this manually, it suddenly can't find the settings module. It's still looking for it at project.settings. Where might this be hardcoded?
When I run python manage.py runserver, I get this error:
ImportError: Could not import settings 'module.settings' (Is it on sys.path?): No module named settings
When I run django-admin.py help <some subcommands> I get this error:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I'm sure somewhere I have the path to the settings set, but I can't find it. Any ideas?
I've used these places in the past:
You might have set the PYTHONPATH environment variable in you .bashrc / .bash_history / some other shell startup file
You might have set said variable in your virtualenv in the $virtualenv/bin/activate script
You migth have symlinked your project from within $virtualenv/lib/pythonx.y/site-packages
The error is because of the following possibilities:
You aren't running the command from your project root i.e. project/ , not project/project.
Have not the project root i.e. project/ added to PYTHONPATH
Your manage.py isn't at project root i.e. project/ , instead it is in project/project.
Possible Solutions:
add the project root i.e. project/ to PYTHONPATH - because manage.py tries to do the equivalent of an import project.settings and for that to happen it needs to find project in sys.path
if you have the project root (project/) listed in PYTHONPATH - issue must be manage.py importing settings in form project.settings, where you have them both at the same level i.e. it has to be just settings - so move your manage.py one level up, to the project root, project/ from the current project/project.
set DJANGO_SETTINGS_MODULE to project.settings.
project.settings indicates that the setting module is one dir lower than manage.py - as in your case you're having both manage.py and settings.py at the same level i.e. project/project/
That is you have not used the default django project structure.
Anyways you can check /export settings just before running 'runserver' command: (from Official Django documentation - setting DJANGO_SETTINGS_MODULE)
Example (Unix Bash shell):
export DJANGO_SETTINGS_MODULE=mysite.settings
django-admin.py runserver
Example (Windows shell):
set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin.py runserver
Suggestion: Try to abide by the conventional project structure (atleast till you become an expert). i.e. keep manage.py one dir level up the other project files .. settings.py, urls.py , etc