[Python] Django 시작하기
- 파이썬용 백엔드 웹 프레임 워크
//-----------------------------------------------------------------------------
결론 : 템플릿 기능이 너무 불편해서 사용하기 힘듬
아래 템플릿 항목 참고
//-----------------------------------------------------------------------------
https://wikidocs.net/91422
장고 설치 
pip install django 
- 장고 프로젝트 생성 
django-admin startproject web_study 
- 데이터베이스 생성 (Sqlite3) 
python manage.py migrate 
- db.sqlite3 파일 생성됨 
- 장고 프로젝트 실행 
python manage.py runserver  
http://127.0.0.1:8000/ 으로 접속 
//------------------------------------- 
- 앱 만들기 
python manage.py startapp main 
- 설정 수정 
web_study/settings.py 파일 수정, main 추가 
INSTALLED_APPS = [ 
... 
  'main', 
//------------------------------------- 
페이지 만들기 
main/templates/index.html 파일 생성 
<!DOCTYPE html> 
<html lang="ko"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
    장고 첫페이지! 
</body> 
</html> 
//------------------------------------- 
- main/view.py 수정 
def index(request): 
    return render(request,'index.html') 
//------------------------------------- 
- main/urls.py 생성 
from django.urls import path 
from . import views 
urlpatterns = [ 
    path('', views.index, name='index'), 
] 
//------------------------------------- 
- web_study/urls.py 파일 수정 
urlpatterns = [ 
    ... 
    path('', include('main.urls')), 
] 
//----------------------------------------------------------------------------- 
페이지 추가 
https://wikidocs.net/91420
- main/templates/index.html 파일 생성 
- main/view.py 수정 
def board(request): 
    return render(request, 'board.html') 
- main/urls.py 수정 
urlpatterns = [ 
    path('', views.index, name='index'), 
    path('board', views.board, name='board'), 
] 
//----------------------------------------------------------------------------- 
정적 파일 로드 
- javascript 파일 로드 방법 
- settings.py 수정 
STATIC_URL = "static/" 
STATICFILES_DIRS = [ 
    BASE_DIR / "static", 
] 
- *.html 파일 수정 
{% load static %} 
<script src='{% static "js/libs.js" %}?v=2'></script> 
//----------------------------------------------------------------------------- 
Model 사용법 
- models.py 수정 
class User(models.Model): 
    id = models.IntegerField() 
    name = models.CharField(max_length=100) 
# 테이블 이름 지정 
    class Meta: 
        db_table = "mj_users"   
- views.py 수정 
from .models import User 
def quiz(request): 
    users = User.objects.all() 
    return render(request, 'quiz.html', {'users':users}) 
- *.html 수정 
   <ul> 
        {% for list in users %} 
        <li>{{ list.name }}</li> 
        <li>{{ list.tel }}</li> 
        {% endfor %} 
    </ul> 
//----------------------------------------------------------------------------- 
템플릿 
문자열 합치기 
https://stackoverflow.com/questions/4386168/how-to-concatenate-strings-in-django-templates
{% with "shop/"|add:shop_name|add:"/base.html" as template %} 
{% include template %} 
{% endwith %} 
//------------------------------------- 
- 모든 변수를 템플릿으로 넘기기 
https://rubayeet.wordpress.com/2009/10/31/django-how-to-make-a-variable-available-in-all-templates/
RequestContext 설치 
from django.templates import RequestContext 
def my_view(request): 
   #view code 
   return redner_to_response('my_template.html', {'foo:bar'}, \ 
                              context_instance=RequestContext(request)) 
//------------------------------------- 
템플릿 내에서 함수 실행 
https://medium.com/swlh/custom-functions-in-django-template-tags-64b73037f2a6
필터로 별도로 만든후 load 해서 사용