Django notes

From Federal Burro of Information
Jump to navigationJump to search

http://dustinfarris.com/2012/09/29/setting-up-a-django-server-with-gentoo.html

the process for tuning the schema:

update the model: scm.py in this case.

python manage.py makemigrations scm
python manage.py sqlmigrate scm 0004
python manage.py migrate scm
python manage.py runserver


Abrdgd Tutorial

go to project dir:

cd C:\users\david thornton\projects\sc2

check that django is installed and working

python -c "import django; print(django.get_version())"

make a new project:

django-admin.py startproject sc2

Database setup (mysql)

edit sc2/settings.py
DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'sc2',
        'USER': 'sc2user',
        'PASSWORD': 'sc2password',
        'HOST': '127.0.0.1',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

hydrate app, creates database, both for the app and for various default INSTALLED_APPS:

python manage.py migrate

this should do a bunch of stuff , including database CREATE work.

Start the dev server for the first time:

python manage.py runserver

alternates:

python manage.py runserver 0.0.0.0:8000

Create a new app in your porject:

python manage.py startapp polls

create the model: polls/models.py

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text


Add the app to the settings.py file:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

Crest the Migration of the app to the project:

python manage.py makemigrations polls

Run the sql changes:

python manage.py sqlmigrate polls 0001

run the migration

python manage.py migrate