DJango startup commands

DJango startup commands

Hi! I have been learning django these days. I came across some basic commands that every beginner should know about. 

Whenever I work on a python project I prefer (and recommend) to work in a virtual environment. For virtual environment you may either use virtualenv, virtualenvwrapper, but I prefer pipenv. More detail about all three of them is available here.

To start a project in django first make a directory say “project” and change directory into it.

$ mkdir project && cd project

Now install django through “pipenv”:

$ pipenv install django

After django is installed, start the virtual environment with:

$ pipenv shell

Then start the project:

$ django-admin startproject newsite

The output of “tree” in the project directory would be like:

$ tree
.
├── newsite
│   ├── manage.py
│   └── newsite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── Pipfile
└── Pipfile.lock

2 directories, 7 files

Now get into the “newsite” directory just created:

$ cd newsite

Now populate the basic database with:

$ python manage.py migrate

or as manage.py is executable you may go with

$ ./manage.py migrate

A new database (db.sqlite3) file in the directory would be created.

Now run the web app with:

$ python manage.py runserver

You may go to your localhost IP to check your empty django project working.

To make an app inside your project, you must be inside “newsite” directory (where manage.py is present). To create an app use:

$ python manage.py startapp newsite_app1

If you’ll make some models inside your “newsite_app1” and want to create corresponding database tables inside your django project database, then you’ll need to makemigrations and migrate your models with:

$ python manage.py makemigrations $ python manage.py migrate

If you’ll have to go to the admin panel of your django app by appending “/admin” in the localhost URL (i.e 127.0.0.1.8000/admin). Here you’ll need a username and password to log in. For this, first create a superuser (admin) for mysite.

$ python manage.py createsuperuser
Username (leave blank to use 'shiva'): 
Email address: shiva@example.com
Password: 
Password (again): 
Superuser created successfully.

I’m keeping this post short. For any query and suggestions to improve this post with more django commands and usage of manage.py, please feel free to write in the comments section below.

Thanks for reading!

 

Advertisements

One thought on “DJango startup commands

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s