how to call different settings from manage.py in django - python

I'm trying to call environment specific settings in django.
I found that you can do something close in django admin according to: https://docs.djangoproject.com/en/2.0/topics/settings/#the-django-admin-utility
I tried this with the manage.py:
python3 manage.py runserver --settings=mysite.settings.prod_settings
I get the error:
ModuleNotFoundError: No module named 'mysite.settings.prod_settings';
'mysite.settings' is not a package
How can I call environment specific settings?
Thanks

I changed the command to:
python3 manage.py runserver --settings=mysite.prod_settings
because I have a file called prod_settings.py and it worked.

Related

How to run the django-admin manage.py runserver

I just want to call this command : django-admin manage.py runserver but it always fails and it gives me this message instead:
(No Django settings specified. Unknown command: 'manage.py')
what can I do ?
Go to the folder where your manage.py file is located nad run
./manage.py runserver or python manage.py runserver
The real command is:
python manage.py runserver
your version with django is incorrect.
django-admin using for creating project
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
After that's enough to run the below command for running the project:
➜ django-admin runserver
or run the below command in the base directory of project:
➜ python manage.py runserver

How to run a django project without manage.py

Basically I downloaded django project from SCM, Usually I run the project with with these steps
git clone repository
extract
change directory to project folder
python manage.py runserver
But this project does not contains manage.py , how to run this project in my local machine???
br
You'll have to create a manage.py file manually if you wanted to use its commands in the same way you're accustomed to. You can modify django's manage.py template to include your project's settings.
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
Most likely, this is not supposed to be a complete project, but a plugin application. You should create your own project in the normal way with django-admin.py startproject and add the downloaded app to INSTALLED_APPS.
You can use uwsgi to run a django project.
First install uwsgi using:
pip install uWSGI
Go to project folder and enter this in terminal:
# Replace server with whatever is your project name
uwsgi --http :8000 --module Server.wsgi
First create a virtual environment and install Django. Now you have django-admin.py available in your system.
django-admin is Django’s command-line utility for administrative tasks.
$ django-admin startproject name [directory] create a Django app directory structure for the given app name in the current directory or the given destination.
You can provide the path to a directory with a custom app template file or a path to a compressed file (.tar.gz, .tar.bz2, .tgz, .tbz, .zip) containing the app template files.
$ django-admin startproject --template=/Users/jezdez/Code/my_app_template myapp
Django will also accept URLs (http, https, ftp) to compressed archives with the app template files, downloading and extracting them on the fly.
For example, taking advantage of GitHub’s feature (or other SCM) to expose repositories as zip files, you can use a URL like:
$ django-admin startproject --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp
$ cd my_proj
$ touch manage.py
Put this content into manage.py like #Sayse said:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
# CHANGED manage.py will use development settings by
# default. Change the DJANGO_SETTINGS_MODULE environment variable
# for using the environment specific settings file.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.development")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Install dependencies and run migrations.
$ pip install -r requirements.txt
$ python manage.py migrate
$ python manage.py runserver
Hope I help!

python: can't open file 'manage.py' when I create django app

I created a project named testdj, and under the directory /tmp/python-test/testdj/testdj/, I want to create a django app, so I use the below command:
aircraftdeMacBook-Pro:testdj ldl$ python manage.py startapp TestModel
python: can't open file 'manage.py': [Errno 2] No such file or directory
But the the upper error, can't open file 'manage.py' there.
I find the stack overflow, there is some the same error, but they are not when creating app occurs.
i guess you are using django app,
so first
pip install django
django-admin startproject project_name
django-admin startapp app_name
python manage.py runserver

Can't seem to setup my Django project properly

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.

Issues in initial setup for django project

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.

Categories