app 생성
python ./manage.py startapp 앱이름
서버 실행
python ./manage.py runserver 포트번호
마이그레이션
python ./manage.py makemigrations
python ./manage.py migrate
관리자 계정 생성
python ./manage.py createsuperuser
주소/admin으로 관리자 db 접근가능
app생성하고 해야되는 것
app만들고 프로젝트파일 settings가서 INSTALLED_APPS에 "member.apps.MemberConfig",
이런식으로 추가해주고
python ./manage.py makemigrations
python ./manage.py migrate
실행
database에 sqlite연결
admin에서 데이터베이스 생성
app의 admin.py에 가서
import member.models
admin.site.register(member.models.memberTable)
def __str__(self):
return self.title+","+self.content+","+(self.writeDate)
경로 html 렌더링
return render(req, "member/start.html")
데이터 넘기고싶으면 data를 딕셔너리{}형태로 전달
간단한 html 그대로 렌더링
return HttpResponse("<body>" +
"메인페이지" +
"<br/>" +
"<a href=start>to start</a>"+
"</body>")
- http요청이 있을 때 반드시 template을 거쳐야한다. X
- http요청이 있을 때 반드시 views 거쳐야한다. O
- http응답은 일반적으로 html으로 한다. O
- http응답은 json으로도 가능하다. O
- http응답을 json으로 하고자 하는 경우 views에서 return한다. O
- ajax를 사용하는 곳은 장바구니, 댓글, 여러 외부 API통신할 때 이다. O
- json을 대체할 수 있는 것은 html이다. X
- http요청이 들어왔을 때 urls.py를 거쳐 views.py를 거쳐 template에 만들어놓은 html파일을 호출하여 http응답한다. O
- ajax로 요청시 html로 응답하는 경우 전체코드를 구현한다. X
- django에서 컨트롤러 역할은 urls이다. X
python3 manage.py shell >> from app이름.models import 클래스명
전체조회
>> 클래스명.objects.all()
특정값 조회
>> 클래스명.objects.filter(addr = 'mulae') >> 클래스명.objects.filter(tel = '011', name = 'park')
특정값 제외 조회
>> 클래스명.objects.exclude(name = 'kim')
삭제
>> 클래스명.objects.filter(addr = 'mulae').delete
추가 or 업데이트
>> 변수 = 클래스명(name = 'kim', tel = '014', addr = 'samsung') >> 변수.save()
카운트
>> 클래스명.objects.all().count()
첫번째, 마지막
>> 클래스명.objects.all().first() >> 클래스명.objects.all().last()
오름차순, 내림차순
>> 클래스명.objects.all().order_by('name') >> 클래스명.objects.all().order_by('-name')