I am reading a book ("Learning Django Web Development" by Sanjeev Jaiswal and Ratan Kumar) on Django, but the book is based on an earlier version of Django (prior to version 1.9). In order to populate the database with tables, the book uses the syncdb command:
$ python manage.py syncdb
Then the book says that terminal will prompt you to create a superuser account.
the syncdb command is no longer used in Django version 1.9 and up. After some research, it seems as if the migrate command populates the databse with tables, but it does not prompt the creation of a superuser account. How can I do this in Django 1.9.6?
I think you want to run these commands:
python manage.py makemigrations creates migration files based on your models
python manage.py migrate will create the tables in your db based on the migration files created
(see docs for more details on database migrations)
python manage.py createsuperuser will create a superuser for your application in the database (docs)
$ python manage.py migrate
$ python manage.py createsuperuser
https://docs.djangoproject.com/en/1.9/ref/django-admin/
For Django 2
User.objects.create_superuser(username='name',email='email',password='password')
From the docs
create_superuser(username, email, password, **extra_fields)
Same as create_user(), but sets is_staff and is_superuser to True.
Which can be embedded in a script, called from a command line or triggered via an API
first run
$ django-admin startproject mysite
in cmd prompt,then apply migration by
cd mysite
mysite:
python manage.py makemigrations
then
python manage.py migrate
after that
python manage.py createsuperuser
First we’ll need to create a user who can login to the admin site. Run
the following command:
$ python manage.py createsuperuser
Enter your desired username and press enter. Username: admin
You will then be prompted for your desired email address:
Email address: admin#example.com
The final step is to enter your password. You will be asked to enter
your password twice, the second time as a confirmation of the first.
Password: **********
Password (again): *********
Superuser created successfully.
$ python manage.py createsuperuser
It will ask username and password
http://127.0.0.1:8000/admin/
see
https://www.tutorialshore.com/create-new-admin-user-in-django/
you create superuser with this :
python manage.py createsuperuser
this will create superuser for you and you can create many superuser .
but notice before you can make super user you must run these command :
python manage.py makemigrations
and them :
python manage.py migrate
Related
I'm following an online tutorial in order to learn Django/Python. I'm using PyCharm Community Edition as my IDE. On Windows 10.
When I run python manage.py startapp myapp at the (venv) prompt in terminal window , no error is shown, and \myapp folder is created with the expected content. However, the file db.sqlite3 is not created, and I can't follow through the rest of the tutorial.
What might be going wrong here?
Thank you very much.
when you start a new django app no database must created.
you can run command
python manage.py migrate
to generate database for your project.
default database is sqlite and stored in file named db.sqlite3
Command python manage.py startapp myapp does not create db.sqlite3.
Run:
python manage.py makemigrations
python manage.py migrate
It will automatically create one if not present.
I have created django python app, and when I press F5 , it pops up a python shell which says
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
shell
showmigrations
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
Press any key to continue . . .
But it does not open any browser and app is not running, when I hit any key , it just stops the app.
I am wondering what is the issue. Weird thing is few days ago this app was working fine and I didn't do anything after that.
I resolved the issue. The issue was with the port 8000, my system was not allowing me to access port number: 8000.
So I went to VisualStudio ---->Debug ----> Project properties at bottom ---> Debug --->enter port number : 5000
and then it worked like charm.
I have a problem with manage.py - it returns 'help' instead of creating an app. I'm quite new in Django so it could be obvious.
I've created a Django project using PyCharm.
New Project../Django../Projct
So PyCharm created a directory called Projct which looks like:
Projct/
Projct/
__init__.py
settings.py
urls.py
wsgi.py
Templates/
manage.py
Now, I want to start on project so I change a name of the SQLite database and do this command:
...PycharmProjects/Projct/manage.py startapp projct
Cmd returns:
Available subcommands
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
shell
showmigrations
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
I've already tried syncdb and it returns the same list.
Do you know where is the problem?
Try to do the following in Pycharm Ctrl+Alt+R, when the new window comes up, type in runserver and hit enter.
By mistake I deleted all the migration files from all apps on production server.
Now running python manage.py makemigrations and then python manage.py migrate raises:
"field already exists in the database"
error for each field, What should I do ?
django version: 1.7.7
If you already migrated and then deleted.Then fake will help you
python manage.py migrate --fake
you have to fake the migration, the next command may help you:
python manage.py migrate 'name_app' 'name_migration' --fake
Example:
python manage.py migrate books 005_auto_20150505 --fake
I have an existing django app and would like to add a field to a model.
But because the website is already in production, just deleting the database is not an option any more.
These are the steps I took:
pip install south
added 'south' to INSTALLED_APPS
python manage.py syncdb
python manage.py convert_to_south [myapp]
So now I have the initial migration and south will recognize the changes. Then I added the field to my model and ran:
python manage.py schemamigration [myapp] --auto
python manage.py migrate [myapp]
Now I have the following migrations:
0001_initial.py
0002_auto__add_field_myapp_fieldname.py
Which commands should I run on my production server now to migrate? Also should I install south first and then pull the code changes and migrations?
first is to fake the initial migration:
python manage.py migrate [yourapp] --fake 0001
then you can apply the migration to the db
python manage.py migrate [yourapp]
I'm assuming you ran convert_to_south on development, in which case production still wouldn't be aware of the migrations yet. convert_to_south automatically fakes the initial migration for you! If you were to just run migrate on production without faking, it should error.