2017년 12월 4일 월요일

파이썬의 유명한 웹 프레임워크인 django가 2.0으로 업데이트 되었습니다.

지금 테스트 중인데 아래의 블로그에 특징이 잘 정리되어 있습니다.

http://raccoonyy.github.io/django-2-0-release-note-summary/


비주얼스튜디오코드로 편집하는 화면입니다.



모바일에 대한 지원이 강화되었습니다. 작은 화면에서 관리 페이지 보기 입니다.


교재에 업데이트될 스크립트 입니다. 버전이 올라갔지만 변경된 부분은 약간입니다.
(장고를 설치)
pip install django

(폴더를 하나 생성하고 그 폴더로 이동한다)
cd \
mkdir c:\django
cd c:\django

(웹폴더를 생성)
django-admin startproject mysite

(웹서버 기동)
python manage.py runserver

(polls앱 생성)
python manage.py startapp polls

(웹서버 기동)
python manage.py runserver

(데이터베이스 동기화)
python manage.py migrate

(polls앱 동기화 추가)
python manage.py makemigrations polls

(SQL구문 보기)
python manage.py sqlmigrate polls 0001

(동기화)
python manage.py migrate

(쉘환경에서 API연습하기)
python manage.py shell
import django
django.setup()
from polls.models import Question, Choice
from django.utils import timezone
q = Question(question_text="What?", pub_date=timezone.now())
q.save()

Question.objects.all()
quit()

(모델에 약간의 코드를 추가한 후에 다시 쉘 환경에 접속)
python manage.py shell

from polls.models import Question, Choice
Question.objects.all()

q = Question.objects.get(pk=1)
q.choice_set.all()
q.choice_set.create(choice_text='Not much', votes=0)
q.choice_set.create(choice_text='The sky', votes=0)
c = q.choice_set.create(choice_text='Just hacking', votes=0)
c.question
c = q.choice_set.filter(choice_text__startswith='Just hacking')
c.delete()
q.choice_set.all()
quit()

(views.py를 아래와 같이 작성한다.)
from django.shortcuts import render
from django.http import HttpResponse
from .models import Question
from django.template import loader

# Create your views here.
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list':latest_question_list,
    }
    return HttpResponse(template.render(context, request))

def detail(request, question_id):
    return HttpResponse("상세 보기 %s" % question_id)

def results(request, question_id):
    return HttpResponse("결과 보기 %s" % question_id)

def vote(request, question_id):
    return HttpResponse("투표 하기 %s" % question_id)


(polls폴더에 templates폴더를 만들고 다시 여기에 polls를 만들고 index.html파일을 생성한다.
c:\django/mysite/polls/templates/polls/indexhtml이 전체 경로임)
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}



댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.

요즘 새로운 과정을 기획하면서 react.js + next.js를 OpenAI API와 같이 사용하는 과정을 만들고 있습니다. ㅎㅎ

 오랜만에 웹 기술들을 공부하니 재미있네요. ㅎㅎ  쭉 파이썬 과정들을 운영하고 있었는데 좀 더 범위를 넓혀서 아이폰 앱 개발과정 with ChatGPT,  웹 프로그래밍 with ChatGPT, AI시대의 AI어시스턴트 활용하기와 같은 글을 쓰고, ...