Skip to content

Instantly share code, notes, and snippets.

@palawer
Last active October 12, 2016 21:24
Show Gist options
  • Select an option

  • Save palawer/8da9c39cc580ebd712c9 to your computer and use it in GitHub Desktop.

Select an option

Save palawer/8da9c39cc580ebd712c9 to your computer and use it in GitHub Desktop.
Django cheat sheat

Django cheat sheet

New project

django-admin startproject mysite
python manage.py migrate
python manage.py runserver
python manage.py startapp app_name

Migrations

python manage.py makemigrations app_name
python manage.py sqlmigrate app_name migration_numer
python manage.py migrate

settings.py

Database setup on Mac with MAMP

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '8889',
    }
}

Templates

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Static files

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/'

Models

models.CharField()
models.TextField()
models.IntegerField()
models.FloatField()
models.URLField()
models.DateField()
models.DateTimeField()
models.SlugField()
models.ForeignKey(Model)
models.OneToOneField(Model)
models.ManyToManyField(Model)

Urls

url(r'^$', views.index, name='index'),
url(r'^some_page/', views.some_view, name='some_view'),
url(r'^some_page/(?P<param_name>[0-9]+)/$', views.other_view, name='other_view'),
url(r'^some_page/(?P<param_name>\d+)/$', views.another_view, name='another_view'),
url(r'^some_page/(?P<param_name>\w+)/$', views.another_view2, name='another_view2'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment