티스토리 뷰
(macOS)[python] localhost(127.0.0.1:8000) main page에 "Welcome to visit django:jinoblog2019" 출력하기
jinozpersona 2019. 10. 10. 16:13현재 jinozblog2019 project 폴더의 구성을 살펴보면 다음과 같다.
- virtualenv : jbe -> bin, include, lib 구성됨
- django 설치 : jbe/lib/python3.7/site-packages/django
project 생성에 사용된 명령어 django-admin.py는 django 하위폴더인 jbe/lib/python3.7/site-packages/django/bin에 들어있다.
django model, template, view, urls등의 module을 구성하면서 기본으로 import된 contrib 폴더는 다음과 같이 구성되어있다.
django의 module/mothod는 django 폴더에서 확인이 가능하니 궁금하면 자주 들여다보는 것이 좋다.
- project 위치 : jbe/mysite/jinozblog2019
manage.py를 포함한다.
jblog : project 생성 후 기본 설정 module을 포함하는 폴더. settings.py, urls.py, wsgi.py(Web Server Gateway Interface)
- project 내 app(post) 위치 : jbe/mysite/jinozblog2019/post
admin.py, apps.py, models.py, tests.py, views.py와 migrations 폴더로 구성되어있다.
# 개발서버를 실행해보자
(jbe)$ python3 manage.py runserver
browser 주소창에 127.0.0.1:8000을 입력하면 다음과 아래 왼쪽과 같은 페이지가 보인다.
우측 페이지는 좌측 페이지의 source가 되는 template이며 다음 폴더에 위치한다. django/views/templates/default_urlconf.html
우측 페이지 상단의 {% load i18n %}는 template tag로 다국어 지원을 선언하고 {% trans ... %}로 적용한다.
settings.py의 LANGUAGE_CODE = 'ko-kr' 로 되어있기 때문에 좌측 처럼 한글로 번역되어 나타난다.
# 개발서버가 실행되면 보여지는 기본 페이지에 "Welcome to visit django:jinoblog2019"를 출력해보자.
HTTP request : localhost, 주소창에 127.0.0.1:8000을 입력했을 때 등록된 urls.py path에 연결된 views.py 내용을 출력한다.
views 생성 : jinozblog2019/post/views.py에 method 작성, django/http 폴더의 HttpResponse class를 import하여 작성
urls 연결 : jinozblog2019/jblog/urls.py에 path 등록
--jinozblog2019/post/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def welcomemessage(request):
return HttpResponse("Welcome to visit django:jinoblog2019")
--jinozblog2019/jblog/urls.py
from django.contrib import admin
from django.urls import path
from post.views import welcomemessage
urlpatterns = [
path('admin/', admin.site.urls),
path('', welcomemessage),
]
작성하면서 실행 중인 개발 서버의 오류 메세지들을 확인하면서 수정해나가면 수월하다
browser를 새로고침하면 다음과 같이 출력을 확인할 수 있다. 우측 화면은 chrome 개발자모드(alt+command+i)
ref. template/home.html을 생성하여 render를 이용한 호출
HTTP request : 127.0.0.1:8000/home을 입력했을 때 등록된 urls.py path에 연결된 views.py 내용을 출력한다.
template/home.html 생성 : "이 페이지는 templates/home.html을 호출하여 작성되었습니다."
post/templates/ 생성 : 초기 app을 설치하면 templates 폴더가 없기 때문에 editor에서 폴더를 추가한다.
post/templates/home.html 생성 : editor에서 home.html 파일을 생성하여 문구를 작성한다.
views 생성 : jinozblog2019/post/views.py에 method 작성, django/shortcuts module의 render 함수를 import하여 작성
urls 연결 : jinozblog2019/jblog/urls.py에 path 등록
--jinozblog2019/post/templates/home.html
"이 페이지는 templates/home.html을 호출하여 작성되었습니다."
--jinozblog2019/post/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def welcomemessage(request):
return HttpResponse("Welcome to visit django:jinoblog2019")
def homemessage(request):
return render(request, 'home.html')
--jinozblog2019/jblog/urls.py
from django.contrib import admin
from django.urls import path
from post.views import welcomemessage, homemessage
urlpatterns = [
path('admin/', admin.site.urls),
path('', welcomemessage),
path('home/', homemessage)
]
개발서버에 오류메세지가 없으면 주소창에 127.0.0.1:8000/home를 입력하면 다음과 같이 결과를 확인할 수 있다.
우측 화면은 chrome 개발자모드(alt+command+i)
----------------------추가사항 : jinozblog2019/post/templates 경로 추가 -------------------
--jinozblog2019/jblog/settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# jinozblog2019/post/templates 폴더이름 'templates'를 경로에 추가해준다.
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')
중략
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
TEMPLATES_DIR, # 위에서 지정한 변수를 추가해준다.
],
'python > Django' 카테고리의 다른 글
(macOS)[python] django post_list site에 구현하기 - 2/2 (0) | 2019.10.14 |
---|---|
(macOS)[python] django post_list site에 구현하기 - 1/2 (0) | 2019.10.11 |
[python] django(장고, 웹 프레임워크), MTV Architecture: Model-Template-View (0) | 2019.10.08 |
(macOS)[python] django superuser 등록 및 app 설치 (0) | 2019.10.07 |
(macOS)[python] django 설치 (0) | 2019.10.07 |
- Total
- Today
- Yesterday
- git
- Pandas
- CSV
- vscode
- Model
- Regression
- DS18B20
- raspberrypi
- 확진
- 코로나19
- sublime text
- DAQ
- server
- template
- pyserial
- github
- Templates
- SSH
- 코로나
- analysis
- MacOS
- COVID-19
- arduino
- ERP
- 라즈베리파이
- Python
- Raspberry Pi
- 자가격리
- r
- Django
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |