Tips

Create new project

django-admin startproject mysite
python manage.py startapp polls

Using django in python cmd

from django.conf import settings
django.setup() # or settings.configure()

Migration

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

Django dump data of django.contrib.auth.Group

Note: deprecated from Django 1.7 !

$ python manage.py dumpdata --format=json auth.Group > fixtures.json

Django migration for auth

$ python manage.py makemigrations account
$ python manage.py makemigrations --empty account

Now adding your code to load initial data in RunPython section:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations


def forwards_func(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    db_alias = schema_editor.connection.alias
    Group.objects.using(db_alias).bulk_create([
        Group(name='Viewer')
        Group(name='Editor'),
        Group(name='Admin'),
    ])


class Migration(migrations.Migration):
    dependencies = [
        ('account', '0001_initial')
    ]

    operations = [
        migrations.RunPython(
            forwards_func,
        ),
    ]

Then:

$ python manage.py syncdb

The '__first__' and '__latest__' can use in dependencies section.

https://code.djangoproject.com/ticket/23422

https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies

https://docs.djangoproject.com/en/1.7/ref/migration-operations/

https://docs.djangoproject.com/en/dev/ref/django-admin/#loaddata-fixture-fixture

how to reset django admin password?

$ manage.py changepassword <user_name>

Run server from Python script

#!/usr/bin/env python
import os
from django.core.management import call_command
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

application = get_wsgi_application()
call_command('runserver', '127.0.0.1:8000')

Static files handling

$ vim settings.py

# static files configs
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
STATIC_URL = '/st/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

INSTALLED_APPS = (
    'django.contrib.staticfiles',
    )

Get the static files URL in view

from django.contrib.staticfiles.templatetags.staticfiles import static
url = static('x.jpg')
# url now contains '/static/x.jpg', assuming a static path of '/static/

https://docs.djangoproject.com/en/1.8/hkowto/static-files/

https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/

http://stackoverflow.com/questions/11721818/django-get-the-static-files-url-in-view

Testing email sending

from django.core import mail
settings.EMAIL_USE_TLS = False
settings.EMAIL_PORT = 25
settings.EMAIL_HOST = 'mail.example.com'
settings.EMAIL_HOST_USER = '[email protected]'
settings.EMAIL_HOST_PASSWORD='<PASSWORD>'
mail.send_mail('Subject', 'Body','[email protected]', ['[email protected]'], fail_silently=False)

Translation

$ django-admin makemessages -a
$ django-admin compilemessages
from django.utils.translation import ugettext
from django.utils import translation

translation.activate('fa')
txt = 'Hello'
ugettext(txt)
'سلام'