A guide to setting up Django, together with some basic cheat sheet commands
pip install Django==1.11.9
python -m pip install Django
python -m django --version
pip install virtualenv
virtualenv project_name
source bin/activate
python3 -m venv virtual_environment_name
deactivate
django-admin startproject project_name
python manage.py showmigrations
python manage.py makemigrations
python manage.py migrate
python manage.py startapp application-name
Please note that a single project
can have multiple applications
inside it
Do not forget to include the new project in settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'application-name'
]
python manage.py collectstatic
vim project-name/settings.py
These are the settings of the entire project
not the applications
!
python manage.py createsuperuser
The command will ask for username, password and email to be provided If you have Django admin module enabled, this user will be able to login in the panel
python manage.py runserver 0.0.0.0:8000
All models, which you would like to be shown in the Django admin, must be manually specified in the admin.py
file inside your application directory. Below is an example with a model named Product
class Product(models.Model):
# Name of the product
name = models.CharField(max_length=128, default=None, blank=True, null=True)
# Description of the product
description = models.TextField(max_length=8096, default=None, blank=True, null=True)
# Date when the product is created
date_posted = models.DateTimeField(default=datetime.now)
# Return name: otherwise it is not shown in the Django Admin
def __unicode__(self):
return self.content
# REQUIRED: All custom models which need to be shown in Django admin, must be manually INCLUDED here:
from .models import Product
# REQUIRED: All custom models which need to be shown in Django admin, must be manually also REGISTERED here:
admin.site.register(Product)