목록Web /Django (22)
알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)
view와 template 연동하기 template 이란 html 문서 안에 python 코드를 집어 넣어 동적으로 html 페이지를 작성 도록 하는것을 말합니다. polls 디렉토리에 templates 라는 디렉토리를 만듭니다. Django 는 여기서 템플릿을 찾게될 것입니다. 방금 만든 templates 디렉토리 내에 polls 라는 디렉토리를 생성하고, 그 안에 index.html 을 만듭니다. 즉, template 은 polls/templates/polls/index.html 과 같은 형태가 됩니다 Django 에서 이 template 을 단순히 polls/index.html 로 참조할 수 있습니다 polls/templates/polls/index.html{% if latest_question_l..
Django view 작성하기 polls/views.py def detail(request,question_id): return HttpResponse("You 're looking at question %s "% question_id") def results(request,question_id): return HttpResponse(response % question_id) def vote(request,question_id): return HttpResponse("You're voting on question %s." % question_id) 코드를 추가해 줍니다. polls 모듈에 view를 추가하기 위해서 polls/urls.py from django.conf.urls import url fro..
관리 사이트 app 셋팅 $nano polls/admin.py from django.contrib import admin # Register your models here. from .models import Question admin.site.register(Question) 위와 같이 설정해 주면 메인 admin 사이트에 등록을 십니다. $sudo service apache2 restart 재시작 해주고 관리자 웹사이트에 접속해 봅니다. Questions에 들어가면 여기서 Question object 에 가면 모델 클래스의 파라메터가 나오고 Date published 라는 이름은 우리는 이전에 모델을 코딩할때 사람이 읽기 좋은 형식 이라면서 이해 가지않는 API 설명을 봤었는데 여기에 정보가 링크가 ..
관리자 생성하기 $ cd ~/mysite $ python manage.py createsuperuser Username (leave blank to use 'back'): admin Email address : admin@example.com Password: Password (again): Superuser created successfully. $sudo service apache2 restart 보시면 형식이 뭔가 이상합니다. 그 이유는 css 가 적용 되지 않았지 때문입니다. $sudo nano /etc/apache2/sites-available/000-default.conf ServerName django1 Alias /static/ /home/back/mysite/static/ Require ..
모델 만들기 모델은 DB와 python 클래스 변수를 연결해주는 작업 이라고 생각 됩니다. 모델을 수정 하는것은 models.py 을 수정 하는것입니다. $ nano ~/mysite/polls/models.py 우리가 만드는 단순한 설문조사(poll) 앱을 위해 Question 과 Choice 라는 두개의 모델을 만들어 보겠습니다. from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model)..
Anaconda 가상환경 spyder IDE(python 통합 개발 환경) 적용 리눅스 환경에서 가상 환경에 python 가상 환경 사용에 spyder IDE 를 적용 하겠습니다. $conda create -n djangotest python=3.5 가상 환경 구축후 $ conda install -n djangotest spyder 설치를 해주면 가상환경 bin 폴더 안에 spyder 가 설치 됩니다.
데이터 베이스 처음으로 Django 를 사용 해서 프로젝트를 만들면 Database 중 Sqllite를 사용하게 됩니다. 프로젝트 폴더에 보면 db.sqlite3 파일이 있는것을 확인 할수 있습니다. 하지만 보통 프로젝트를 할때에 mssql,mysql,mariadb,oracle 등등 데이터 베이스를 사용하게 됩니다. 저는 여기서 mariadb 를 사용하겠습니다. mariadb는 mysql와 거의 98% 호환 됩니다. 그냥 설치는 mariadb로 하고 사용법은 mysql 을 따르면 됩니다. mariadb를 사용하는것은 mysql 이 오라클로 넘어가면서 라이센스 정책이 변경되었기 때문에 완전 무료인 mariadb를 사용합니다. $ sudo apt-get install mariadb-server-10.0 사용..
start app 만들기 시작은 일단 가상환경 적용 입니다. $source ~/anaconda3/envs/djangotest/bin/activate djangotest 자신의 django 프로젝트 폴더에 들어 갑니다. $cd ~/mysite/ $ ./manage.py startapp polls 그러면 폴더가 생성 되며 polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py다음과 같은 폴더 구조를 가지게 됩니다. 이제 첫번째 여기서 뷰는 사용자에게 보여주는 측면의 App을 작성 하는것이라고 생각 됩니다. 이제 polls/view.py 파일을 열어 파이썬 코드를 입력 해줍니다. from django.shor..
Django 와 apache2 연동 앞서 기본 프로젝트 만들기 에서 배운것 처럼 django에 $ python manage.py runserver 0.0.0.0:8000 으로 웹 페이지를 서비스 할수 있지만 성능의 문제와 추천하지 않는 방법 입니다. 그래서 웹서버 프로그램을 연동 시켜 하는것이 일반적 입니다. 웹서버 종류로는 여러가지가 있지만 본편적으로 많이 쓰고 자료도 많고 무료 소프트웨어인 apache2에 연동 하고자 합니다. apache2 설치 $ sudo apt-get install apache2위의 명렁어로 apache2를 설치해 줍니다. 설치가 완료 되면 다음과 같이 default page가 뜨게 됩니다. 이제 Django 와 apache2 를 연동 하겠습니다. Django + apache2 ..
지금 부터 학습 루트는 https://django-document-korean.readthedocs.io/ko/master/intro/index.html 위의 사이트의 튜토리얼을 기준으로 작성하고자 합니다. 프로젝트 만들기 설치를 정삭적으로 끝냈다면 $ ~/anaconda3/envs/djangotest/bin/django-admin.py 있습니다. 커맨더에 가상 환경을 적용 했기 때문에 해당 경로를 모두다 적을 필요가 없습니다, $ source ~/anaconda3/envs/djangotest/bin/activate djangotest (가상환경 적용) $ django-admin startproject mysite 현재 경로의 폴더에 mysite 폴더가 생성되며 프로젝트가 만들어 집니다. startpro..