Django notes: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:


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


the process for tuning the schema:
the process for tuning the schema:
Line 11: Line 10:
  python manage.py migrate scm
  python manage.py migrate scm
  python manage.py runserver
  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
<pre>
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',
    }
}
</pre>
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
<pre>
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
</pre>
Add the app to the settings.py file:
<pre>
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)
</pre>
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

Latest revision as of 15:18, 18 September 2014

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