CS Activity/Likelion 11th

[멋쟁이사자처럼 11기] 3회차: Django 기초

Jenn28 2023. 3. 20. 20:36

MTV 패턴

- Model: 데이터 찾기 담당

- Template: 보여지기 담당

- View: 처리 담당


가상환경 생성:

python -m venv 가상환경이름

 

가상환경 실행:

source 가상환경이름/Scripts/activate

 

가상환경 종료:

deactivate

 

장고 설치:

pip install django

 

프로젝트 시작:

django-admin startproject 프로젝트 이름

 

서버 정상 작동하는지 확인

python manage.py runserver

 

manage.py로 앱 생성:

python manage.py startapp 앱 이름

 

ctrl+c 하면 나가짐


프로젝트 이름의 settings.py 파일 들어가서 INSTALLED_APPS에 앱 이름 추가해주기


home.html

<h1>Hello World!!</h1>

base.html

<h1>base 페이지입니다.</h1>

views.py

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')
    
def base(request):
    return render(request, 'base.html')

urls.py

.
.
.
from django.contrib import admin
from django.urls import path
import myapp.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', myapp.views.home), #'' 이 주소로 이동해서 myapp의 views의 home 페이지 불러오기

    #127.0.0.1:8000/base
    path('base/', myapp.views.base),
]

gitignore 사용법

https://www.toptal.com/developers/gitignore/

 

gitignore.io

Create useful .gitignore files for your project

www.toptal.com

 

이렇게 작성하고 코드를 .gitignore 파일에 복붙하기

-> gitignore에 있는 파일/폴더들은 git에 업로드 x (가상환경 venv는 굳이 업로드 안해도 되니)

-> 가장 상위 폴더에 .gitignore 파일 만들어주고 복붙 ex) 0323 폴더

 

 

<오늘의 git 명령어>

 

remove / 폴더 강제 삭제 / 파일명:

rm -rf .git

 

git bash에서 복붙:

shift insert