LoginSignup
7
7

More than 5 years have passed since last update.

Django 初期設定 Intellij Debug まで

Last updated at Posted at 2015-05-30

環境

  • mac
  • mysql インストール済み
  • python 2.7
  • virtualenv
  • django 1.7
  • intellij 12
    • Python, Django Plugin インストール済み

初期設定

# リポジトリ作成
mkdir python-django-mvt
cd python-django-mvt
# virtualenv 作成
virtualenv env
source env/bin/activate
vi requirements.txt
Django==1.7.1
django-debug-toolbar==1.2.2
MySQL-python==1.2.5
# 必要モジュールインストール
pip install -r requirements.txt
# 確認
pip freeze

Django Project 作成 (MySQL 利用)

# プロジェクト作成
django-admin.py startproject cmsproject
cd cmsproject
vi cmsproject/settings.py
# DATABASE 設定を mysql に、タイムゾーンと言語を日本に変更
DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'cms',
         'USER':'user',
         'PASSWORD':'password',
         'HOST':'127.0.0.1',
     }
 }

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'
# DB 初期化
python manage.py migrate
# Admin のユーザ作成
python manage.py createsuperuser
# サーバ起動
python manage.py runserver
# 管理画面アクセス
http://127.0.0.1:8000/admin

Django App 作成

python manage.py startapp cms

Intellij 設定

ここで Intellij にプロジェクトをインポート

  • import project
  • cmsproject
  • SDK + Python SDK に env の環境を指定
  • project structure の Modules の Django で Settings と Manager script を指定(一度削除し作りなおさないとだめな場合あり)
  • Debug 実行
  • サーバが起動する

Model 追加

vi cms/models.py
models.py
from django.db import models

# Create your models here.
class Entry(models.Model):
    title = models.CharField('title', max_length=255)
    contents = models.TextField('contents')

    def __str__(self):
        return "<Entry('%s', '%s', '%s')>" % (self.id, self.title, self.contents)

Migration

# installed app に cms 追加
vi cmsproject/settings.py
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cms',
)
# migrate file 作成
python manage.py makemigrations cms
# 確認
python manage.py sqlmigrate cms 0001
# 実行
python manage.py migrate

テーブルが既にある場合

python manage.py inspectdb

Github

参考

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7