[Day18] 2022-02-21(월) Web Application 1 - Django 환경 셋팅 - 이진만 강사님
[1] Web Application Settings
1. Web Server
2. Python Projcet
- Web application 추가
- django Server
3. 개발 순서
1) Python Project 생성
2) 서드파트 모듈 설치
pip 버전 최신버전으로 업그레이드
django, mysqlclient, django-request-mapping
3) Web Application 환경으로 변환 - terminal 에서 작업
django-admin startproject config .
4) Project 안에 Web Application 생성 - terminal 에서 작업
python manage.py startapp hello
python manage.py runserver 80
5) html 폴더 설정
- Project 아래 폴더 생성
templates - html 파일
static - 이미지, css 등
6) 코드 수정
- config / settings.py 아래와 같이 수정
# Allow Multi IP
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello' # app name
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# mariadb 연동 setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'company',
'USER': 'director',
'PASSWORD': '111111',
'HOST': '127.0.0.1',
'POST': '3306'
}
}
# 파일 하단에 입력
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
- config / urls.py 수정
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('', include('hello.urls')),
]
- hello / urls.py 생성
from django_request_mapping import UrlPattern
from hello.views import MyView
urlpatterns = UrlPattern();
urlpatterns.register(MyView);
- hello / views.py 파일 수정
from django.shortcuts import render
from django.views import View
from django_request_mapping import request_mapping
@request_mapping("")
class MyView(View):
@request_mapping("/", method="get")
def home(self,request):
return render(request,'home.html');
- templates 폴더에 home.html 생성 후
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello Django world !!!</h1>
</body>
</html>
7) 서버 실행 및 출력 확인
- 서버 실행 - terminal에서 실행
python manage.py migrate
python manage.py runserver 80
- Browser 에서 127.0.0.1 로 접속 -> title과 h1에 입력한 텍스트 뜨는지 확인
[2] 화면에 이미지 표현하기
1. web application folder setting
config / settings.py 파일 하단에
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
2. 이미지 파일 추가
- Project 밑에 static 폴더에 img 폴더 생성 후 복사한 이미지 붙여넣기
3. HTML에서 이미지 로딩
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Shop Test</h1>
<img src="{% static 'img/logo.png' %}">
</body>
</html>
4. 서버 실행 및 출력 확인
- 서버 실행 - terminal에서 실행
python manage.py runserver 80
- Browser 에서 127.0.0.1 로 접속 -> 추가한 이미지가 뜨는지 확인
[3] 서버 및 웹페이지 동작 과정
1. 웹브라우저에서 127.0.0.1 요청
1) config/urls.py -> hello/urls.py 실행
2) hello/urls.py -> hello/views.py에 있는 MyView 라는 클래스 실행
3) hello/views.py -> 요청 url이 127.0.0.1/ 이면 home.html 을 웹브라우저에 전송
2. 127.0.0.1 에서 next를 클릭하면 웹브라우저에서 127.0.0.1/next 를 요청
1) config/urls.py -> hello/urls.py 실행
2) hello/urls.py -> hello/views.py에 있는 MyView 라는 클래스 실행
3) hello/views.py -> 요청 url이 127.0.0.1/next 이면 next.html 을 웹브라우저에 전송