Notice
Recent Posts
Recent Comments
Link
초보 개발자
django message 본문
django message
django.contrib에 있는 message를 사용하여 유저가 로그인하거나, 로그아웃하거나 할 때 어떤 메시지를 보여주려고 한다. 먼저 messages.html을 만들어 준 후 아래와 같이 if문을 사용하여 나타내주었다.
{% if messages %}
<ul class="absolute mx-auto left-1/2 top-0 -translate-x-2/4 flex justify-center">
{% for message in messages %}
<li class="message opacity-0 bg-gray-700 rounded-full px-6 py-4 font-medium text-white {% if message.tags %} {{ message.tags }} {% endif %}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
먼저 css는 생략하고 내용물만 보면, messages가 있다면 그 messages안에 있는 개개의 message를 출력하도록 하는 것이다. 또한 li안의 class안에 if문을 작성하여 message.tag가 들어가도록 하였다. 이 message.tag에는 우리가 message를 작성할 때 주는 옵션같은 것이다. 이 부분은 좀있다 설명하겠다.
간단히 설명하자면 아래와 같은 형식으로 사용한다.
messages.tag(request, '할말') 여기서 우리가 tag부분에 적어줄 수 있는 것이 success, info, warning, error 등이 있다.
다른 예로 로그인이 성공했을 땐 아래와 같다.
base.html안에 아래와 같이 messages.html을 넣어줘보자.
이에 그치지 않고 각 태그에 따라 css가 다르게 설정되도록 해주면 더 효과적일 것이다.
앞서 우리가 class안에 if로 tag를 넣어주었떤 이유가 아래와 같이 구별하려고 한 것이다.
'Python > airbnb 클론' 카테고리의 다른 글
django user-editprofile with UpdateView (0) | 2022.03.15 |
---|---|
django userProfile with DetailVeiw (0) | 2022.03.15 |
django login, signup page (0) | 2022.03.13 |
django password_validator (0) | 2022.02.21 |
send email django (0) | 2022.02.18 |