LoginSignup
16
20

More than 5 years have passed since last update.

djangoでの最小構成のwebサイト環境

Posted at

djangoでwebサイトの最小構成環境を作成しようと思います。

環境

  • mac os x 10.9.5
  • python 2.7.11
  • django 1.9
  • mysql 5.6.19
  • pip 1.5.6

django環境構築

とりあえずvirtualenvwrapper等をinstallして環境を整える。

# sudo pip install virtualenvwrapper
# mkvirtualenv --python=/path/to/python/2.7.11/bin/python my_site
# pip install django==1.9
# pip install mysql-python
# django-admin startproject my_site
# cd my_site
# mysql -u root
> CREATE DATABASE my_site;
Query OK, 1 row affected (0.00 sec)
> exit

settingsの内容を更新します。
DATABASESの内容をmysqlに変更します
LANGUAGE_CODEをjaに変更します。

my_site/my_site/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILE_DIR = os.path.dirname(os.path.abspath(__file__))  # settingsが存在する階層
BASE_DIR = os.path.dirname(FILE_DIR)  # Build paths階層

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_site',
        'USER': 'root',
        'PASSWORD': '',
    },
}

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Japan'

djangoのDB環境やadmin管理画面のユーザを作成する。

# cd my_site
# python manage.py migrate
# python manage.py createsuperuser
Username (leave blank to use 'yuji.kanazawa'): admin
Email address: admin@email.co.jp
Password: ******
Password (again): ******
# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
April 06, 2016 - 09:21:55
Django version 1.9, using settings 'my_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/admin/ へ接続頂きます。
createsuperuserで設定したユーザ名とパスワードを入力してログインするとdjangoの
デフォルトで提供している管理画面が表示されます。

ちなみに、settingsで変更したLANGUAGE_CODEを変更せず
「en-us」のままにすると、英語の管理画面が表示されます。

view作成

「my_site/my_site/views」フォルダを作成します。
該当のフォルダ内の構成は下記の通りです。
__init__.pyの中身は空で問題ありません。

views
├─__init__.py
├─index.py
└─urls.py
my_site/my_site/views/index.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from django.views.generic import TemplateView
from django.utils.translation import ugettext as _


class IndexView(TemplateView):
    template_name = "index.html"

    def get(self, request, *args, **kwargs):
        context = {
            "title": _("TOPページ"),
            "text": _("ハローワールド"),
        }
        return self.render_to_response(context)
my_site/my_site/views/urls.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from django.conf.urls import url
from .index import IndexView


urlpatterns = [
    url(r'^$', IndexView.as_view(), name='api-index'),
]

上記の対応に伴いsettingsのINSTALLED_APPSへviewsを追加する

my_site/my_site/settings.py
INSTALLED_APPS = [
    ,
]

INSTALLED_APPS += [
    'my_site.views',
]

後、urls.pyにviewsのurlsを登録する

my_site/my_site/urls.py
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^apis/', include('my_site.views.urls')),
]

template作成

「my_site/my_site/templates」フォルダを作成します。

views
└─index.html
my_site/my_site/templates/index.html
{% load i18n %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>{% trans title %}</title>
</head>
<body>
{% trans text %}
</body>
</html>

フォルダ構成

my_site
├─my_site
│ ├─ __init__.py
│ ├─settings.py
│ ├─templates
│ │ └─index.html
│ ├─urls.py
│ ├─views
│ │ ├─__init__.py
│ │ ├─index.py
│ │ └─urls.py
│ └─wsgi.py
└─manage.py

サーバー動作確認

# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
April 08, 2016 - 14:04:01
Django version 1.9, using settings 'my_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/views/
に接続すると下記のようなページが表示されます。

スクリーンショット 2016-04-08 14.50.54.png

Git Hub

上記の最小構成環境をこちらで公開しております。
もし良かったら、cloneしてみて下さい。

今後はmodelを作成してmysqlにデータを流し込めるようにしたいと考えています。
https://github.com/yu-sa/my_site

16
20
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
16
20