Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active February 24, 2024 23:35
Show Gist options
  • Save bradtraversy/0df61e9b306db3d61eb24793b6b7132d to your computer and use it in GitHub Desktop.
Save bradtraversy/0df61e9b306db3d61eb24793b6b7132d to your computer and use it in GitHub Desktop.
Django command cheat sheet

Django 2.x Cheat Sheet

Creating a virtual environment

We need to create a virtual env for our app to run in: More Here Run this command in whatever folder you want to create your venv folder

python -m venv ./venv

Activate the virtualenv

# Mac/Linux
source ./venv/bin/activate

# Windows
venv\Scripts\activate.bat - May need to add full path (c:\users\....venv\Scripts\activate.bat)

Escape from venv

deactivate

Check packages installed in that venv

pip freeze

Install Django

pip install django

Create your project

django-admin startproject PROJECTNAME

Run Server (http://127.0.0.1:8000) CTRL+C to stop

python manage.py runserver

Create an app

python manage.py start app APPNAME

Create migrations

python manage.py makemigrations

Run migration

python manage.py migrate

Collect Static Files

python manage.py collectstatic
@winadiw
Copy link

winadiw commented Nov 18, 2018

i think it should be: python manage.py startapp APPNAME and the create venv command should be using python3 for python version 3: python3 -m venv ./venv

@ghouseshaik528
Copy link

i think it should be: python manage.py startapp APPNAME and the create venv command should be using python3 for python version 3: python3 -m venv ./venv

yes

@ftp5500
Copy link

ftp5500 commented Jan 31, 2019

who installed python 3 we should replace python to python3

@ZeroCoolHacker
Copy link

i think it should be: python manage.py startapp APPNAME and the create venv command should be using python3 for python version 3: python3 -m venv ./venv

Right

Copy link

ghost commented Jul 14, 2019

who installed python 3 we should replace python to python3

No need for that.

@KunalGautam
Copy link

who installed python 3 we should replace python to python3

No need for that.

Some system have python 2.x and python 3.x installed. Where python command points to python2.x binary. Hence python3 is like playing safe in such situation.

@ingafter60
Copy link

This comand 'python manage.py start app APPNAME' will not work.
It should be like this 'python manage.py startapp APPNAME'

@demonhunter3333
Copy link

python manage.py startapp APPNAME

@barefacedbear
Copy link

i think it should be: python manage.py startapp APPNAME and the create venv command should be using python3 for python version 3: python3 -m venv ./venv

if u have only python3 installed u can write python only python3 is basically needed to be written for machines running python 2.x and python 3.x

@pau-lo
Copy link

pau-lo commented May 1, 2020

This is all great but I highly recommend using pipenv for handling your virtual environment. great tool.❤️

@krasow
Copy link

krasow commented Jun 3, 2020

who installed python 3 we should replace python to python3

If you don't want to you python2 (or not inside a virtual env), you can change you bashrc file making a alias so python = python 3. This works for linux based systems.

Place this into ~/.bashrc or ~/.bash_aliases file
alias python=python3
After adding the above in the file, run source ~/.bashrc or source ~/.bash_aliases.

@sarveshgupta67890
Copy link

It's actually python only. On windows python on CLI works as python but on Mac and Linux it is installed as python3.

@enosh-edward
Copy link

enosh-edward commented May 28, 2021

So i tried to learn django today, and my PC has latest version of python and django (3.2.3) and when i have typed the "django-admin startproject (My project name) it is showing an error even though my file path is also in a proper order. Can any one help me with this.

@keshav407
Copy link

  • pip freeze
  • pip freeze > requirements.txt
  • pip install -r requirements.txt

@FMFigueroa
Copy link

FMFigueroa commented Jun 10, 2021

Working from the console with Python:

python manage.py shell

Create new object

from APPNAME.models import CLASSNAME

CLASSNAME.objects.create(key_1 = value_1, key_2 = value_2, key_n = value_n)

@GameCoder22
Copy link

Can you do one for python 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment