스파르타 강의를 보며 만들었던 장고파일을 봤는데...
분명 할 때는 이해가 잘 갔던거같은데
다시보니까 겹치는 이름이 너무 많다.. ㅠㅠ
쉽게 설명해주는게 장점이라면
이런 디테일함에 있어서는 조금 부족한것 같아서 아쉽다 ...
모든게 완벽할 수는 없겠지
01) render() 구문
- 궁금했던 것 -
1. render()구문에서 {}(중괄호) 안에 들어가는 'my_commit'와 html에서 템플릿 구문으로 쓰이는 'my_commit'은 models.py 에있는 db_table 이름이 맞는지?
# 예시
# views.py
def detail(request):
if request.method == 'GET':
all_commit = Commit.objects.all().order_by('-created_at')
return render(request, 'main.html', {'my_commit':all_commit})
# models.py
class Commit(models.Model):
class Meta:
db_table = "my_commit"
# main.html
{% for tw in my_commit %}
<div class="col-md-12 mb-2">
<!-- 중간 생략 -->
</div>
{% endfor %}
답 : db_table 이름이 아니다 never! 단지 {key : value} 형태인 '딕셔너리'로 맞춰주기 위해 쓴 것이다. 따라서 db_table 이름인 'my_commit' 이 아닌 아무 이름을 넣어도 상관이 없다. 단, render() 구문은 html을 받아오는 형식이기 때문에 html에서 사용한 것을 key값으로 받아와야 한다! 아래 예시와 같이 html의 템플릿 구문에서 변수이름을 'commit_'으로 사용한다면 render() 부분을 아래와 같이 변경하면 된다!
# 예시
# main.html
{% for tw in commit_ %}
<div class="col-md-12 mb-2">
<!-- 중간 생략 -->
</div>
{% endfor %}
# views.py
def detail(request):
if request.method == 'GET':
all_commit = Commit.objects.all().order_by('-created_at')
return render(request, 'main.html', {'commit_':all_commit})
render() 구문 사용법
- render(request, html, {딕셔너리})
02) 인자와 쿼리셋 구분
- 궁금한 것-
1. commit_comment = Comment.objects.filter(commit_id=id).order_by('-created_at') 에서 commit_id는 데이터베이스 에 있는 걸 가져오는거같은데 commit_id가 아닌 commit 으로만 해도 작동이 잘되는데 그 이유가 무엇인지?
# views.py
def detail_commit(request, id):
my_commit = Commit.objects.get(id=id)
commit_comment = Comment.objects.filter(commit_id=id).order_by('-created_at')
답: commit_id는 데이터베이스에 있는 걸 가져오는것이 맞다. 즉 쿼리셋을 가져오는것이다! 아래 그림은 models.py에 있는 Comment 클래스의 데이터베이스이다. commit 으로만 해도 작동되는 이유는 예외 처리부분에서 걸리지 않아 운좋게 넘어간 케이스이다. 쿼리셋과 인자를 확실하게 구분 할 줄 모른다면 이러한 부분에서 에러가 날것이다. 밑에는 예시이다.
#예시
def detail_commit(request, id):
my_commit = Commit.objects.get(id=id)
commit_comment = Comment.objects.filter(commit_id=id).order_by('-created_at')
함수 안에 있는 인자인 id는 'id=id'와 'commit_id=id' 에서 두번째에 해당하고, 첫번째 id같은 경우엔 commit_id와 같이 데이터베이스에 있는 쿼리셋을 가져오는것이다
03) Messages Framework
1. Messages Framework
- 1회성 메세지를 내보내는 용도
2. Message class 속성
- message: 실제 메시지
- level: 메시지 레벨을 나타내는 정수
- tags: 메시지 레벨을 나타내는 문자열 조합
Django에서는 기본적으로 settings.py에 들어가 이런 내용이 다 들어가 있기 때문에 별도의 설정없이 사용 가능함
3. Message 추가
# .py
from django.contrib import messages
# 종류
messages.debug(request, '%s SQL statements were executed.' % count)
messages.info(request, 'Three credits remain in your account.')
messages.success(request, 'Profile details updated.')
messages.warning(request, 'Your account expires in three days.')
messages.error(request, 'Document deleted.')
4. Message 띄우기
# html
{% if messages %}
<div>
{% for message in messages %}
{{ message.tags }}! {{ message.message }}
{% endfor %}
</div>
{% endif %}
* 부트스르렙을 이용해 꾸밀 수 있다.
'내일 배움 캠프 > TIL' 카테고리의 다른 글
TIL) 5주차 5일 (0) | 2023.04.15 |
---|---|
TIL) 5주차 4일 (0) | 2023.04.14 |
TIL) 5주차 2일 (0) | 2023.04.11 |
TIL) 5주차 1일 (0) | 2023.04.11 |
TIL) 4주차 4일 - 장고 실무 기초 (0) | 2023.04.07 |