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 %}



댓글 없음:

댓글 쓰기

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

미국에서 만들고 있는 데이터 센터들 - AI시장이 얼마나 커질까?

 말도 안되는 규모로 테슬라, 메타, OpenAI의 스타게이트 연합군에서 짓고 있는 데이터 센터들입니다.  1기가와트, 2기가와트, 5기가와트 ㅎㅎ  앞으로 세상이 정말 더 많이 변할 것 같습니다. 저는 하드웨어를 직접 다루지 않기 때문에 IDC센터를...