1. 데이터 베이스 프레임 생성 (models.py)

cd demo

vi models.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.db import models

from django.db.models import *


# Create your models here.

class Product(Model):

  # primary_key : only one value in this class

  pid = BigIntegerField(primary_key=True)

  # db_index : group value for searching faster

  cat1id = BigIntegerField(db_index=True)

  cat2id = BigIntegerField(db_index=True)

  cat3id = BigIntegerField(db_index=True)

  pname = TextField(default='-')


  status = TextField(default='none')


  def set_status(self, value):

    self.status = value

    self.save()


2. 새로운 모델의 migrations 생성 및 적용

cd ..

python manage.py makemigrations demo

python manage.py migrate demo


3. 데이터 베이스 생성

vi test.db

12345 001 002 003 Poorman T-shirts

12346 001 002 004 Poorman Boots

12347 001 003 003 Poorman Cap

12348 002 003 004 Poorman Watch

vi make_db.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


import os, sys, django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

django.setup()

from catetc.models import *


class ModelObjects():

  def __init__(self, model):

    self.model = model


  def create(self, line):

    line = line.strip()

    words = line.split('\t')

    pid = words[0]

    cat1id = words[1]

    cat2id = words[2]

    cat3id = words[3]

    pname = words[4]

    self.model.objects.create(pid=pid, cat1id=cat1id, cat2id=cat2id, cat3id=cat3id, pname=pname)


  def delete(self):

    self.model.objects.all().delete()



def main():

  db = ModelObjects(Product)

  db.delete()

  for line in sys.stdin:

    line = line.strip()

    if not line:  continue


    db.create(line)


# main

if __name__ == '__main__':

  main()

cat test.db | python make_db.py


5. admin (관리자) 모드에서 볼 수 있게 admin 등록

vi demo/admin.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.contrib import admin

from .models import *


# Register your models here.

admin.site.register(Product)


6. admin 홈페이지에서 데이터 베이스 확인

http://~:8080/admin


1. Django 프로젝트 urls 설정

vi mysite/urls.py

from django.conf.urls import url, include

from django.contrib import admin


urlpatterns = [

  url(r'^admin/', admin.site.urls),

  url(r'', include('demo.urls')),

]

cd demo

vi urls.py

from django.conf.urls import url

from . import views


urlpatterns = [

  url(r'^demo/$',views.demo_base.as_view(), name='demo_base'),

]


2. Django 프로젝트 views 설정

vi views.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.shortcuts import render

from django.views import View


# Create your views here.

class demo_base(View):

  def get(self, request, *args, **kwargs):

    return render(request, 'demo/demo_base.html', context={})


3. Django 프로젝트 html 생성

mkdir -p templates/demo

vi templates/demo/demo_base.html

<!doctype html>

<html lang="ko">

<html>

  <head>

    <meta charset="utf-8">

    <title>Django Demo</title>

  </head>


  <body>

    <a href="http://poorman.tistory.com">

      <img src="https://t1.daumcdn.net/cfile/tistory/2368C93A587F63331A"> &nbsp

      Poorman Tistory

    </a>

  </body>


</html>


- 결과

http://~:8080/demo


0. Django 장고 프로젝트 경로 설정

project_path = './demo'

app_name = 'demo'


1. Django 장고 프로젝트 생성

django-admin startproject mysite $project_path


2. Django 장고 프로젝트 APP 생성

cd $project_path

python manage.py migrate

python manage.py startapp $app_name


vi mysite/settings.py

ALLOWED_HOSTS = [

'192.168.0.1',

]


INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'demo',

]


TIME_ZONE = 'Asia/Seoul'

python manage.py makemigrations $app_name

python manage.py migrate $app_name


3. Django 장고 프로젝트 슈퍼 유저 생성

python manage.py createsuperuser


4. Django 장고 프로젝트 실행

python manage.py runserver 0:8000


- 결과

http://~:8080/


+ Recent posts