Since it is a memorandum & personal use, it is written quite simply. I think beginners should also refer to other articles.
I mainly worked with python 2.7.10 and Django 1.6. Please note the following points when working with python 3 series, Django 1.9
Select the appropriate version when $ pyenv install
Select the appropriate version when $ pip install django
. (The latest 1.9 is included by default)
It feels like a database is created in one shot after writing a model with Django 1.6 $ python manage.py syncdb
.
After writing the model as 1.9
After making a migration file with $ python manage.py makemigrations app name
It feels like creating a DB from a migration file with $ python manage.py migrate
.
That's it (probably).
Install from GitHub
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
(~ / .pyenv is the local directory name. Make it an appropriate name) Write pyenv settings to zshenv, bash_profile, etc.
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
(Rewrite $ HOME
to the installation location of pyenv as appropriate)
Restart Shell for the settings to take effect.
$ exec $SHELL
Install python from pyenv.
$ pyenv install 2.7.10
Set the installed version.
$ pyenv global 2.7.10
$ pyenv rehash
Verification
$ pyenv versions
* 2.7.10 (set by /hoge/.pyenv/version)
Install Django 1.6 from pip
$ pip install django==1.6
Create test project
$ django-admin.py startproject mysite
Server startup
$ cd mysite
$ python manage.py runserver
Go to http: // localhost: 8000 and check if the server is running.
Main References Creating your first Django app, part 1 | Django 1.4 Documentation
Change the settings related to DB. By default, sqlite is used, but this time PostgreSQL is used. See Notes on enabling PostgreSQL with Django | Qiita It is assumed that a new user and database are created by installing the postgresql library etc. referring to the above site.
setting.py
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mysite',
'USER': 'djangouser',
'PASSWORD': 'pass',
'HOST': '127.0.0.1',
'PORT': 5432,
}
}
Create the table used by the application described in INSTALLED_APPS in setting.py by executing the following command.
$ python manage.py syncdb
(The syncdb command looks for the INSTALLED_APPS setting and creates the required table on the database according to the database settings in settings.py.)
$ python manage.py startapp polls
(Polls is the application name)
polls/models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Rewrite the settings to make the project aware of the polls app.
Added polls
inside ʻINSTALLED_APPS`.
setting.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
)
$ python manage.py sql polls
BEGIN;
CREATE TABLE "polls_poll" (
"id" serial NOT NULL PRIMARY KEY,
"question" varchar(200) NOT NULL,
"pub_date" timestamp with time zone NOT NULL
)
;
CREATE TABLE "polls_choice" (
"id" serial NOT NULL PRIMARY KEY,
"poll_id" integer NOT NULL REFERENCES "polls_poll" ("id") DEFERRABLE INITIALLY DEFERRED,
"choice" varchar(200) NOT NULL,
"votes" integer NOT NULL
);
COMMIT;
$ python manage.py syncdb
See Creating your first Django app, part 1 | Django 1.4 Documentation rather than playing with the API.
Launch a python interactive shell.
$ python manage.py shell
(Let's play variously with reference to the above site)
By the way, in the process of playing, polls / models.py was rewritten as follows.
polls/models.py
import datetime
from django.utils import timezone
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
Recommended Posts