[다시, 장고]Muklog - 먹기록 조회(회원별), 먹기록 삭제, 로그인상태 수정

Adela·2020년 7월 18일
0

다시, 장고

목록 보기
5/6
post-thumbnail

오늘 진행한 것들

  • 먹기록 조회 (회원별)
  • 먹기록 삭제
  • 로그인 상태별 로그인/로그아웃 상태 변경

먹기록 조회(회원별)

현재 로그인한 회원이 작성한 글만 보여지도록 수정

  • 임시 홈 화면에서 기록 조회

views

def tempHome(request):
    user = request.user
    # 현재 유저 받아옴 
    if user.is_authenticated:
    # 유저가 로그인되었다면 
        try:
            blogs = Blog.objects.all().filter(user=user)
            # 해당 유저가 작성한 객체만 가져옴 
            return render(request, 'tempHome.html', {'blogs': blogs})
        except Blog.DoesNotExist:
        # 해당 유저가 작성한 기록이 없다면 
            messages.error(request, 'Blog dose not exist')
            # 존재하지 않는다는 메시지 띄우기
    return render(request, 'tempHome.html')

✔ 로그인 한 상태에 따라
✔ 유저가 기록을 작성했는지 안해왔는지에 따라
객체를 불러올 수 있고 없고의 차이가 생기기 때문에 해당 조건에 맞도록 조건문을 걸었다.

html

  • 유저가 로그인했는데, 저장한 기록이 없으면
{% if user.is_authenticated %}
<div>
  {% if messages %}
   {% for message in messages %}
   	{{ message.tags }}
   	{{ message.message }}
   {% endfor %}
  {% endif %}
</div>

📌 하지만 메시지 부분이 잘 작동하지 않으므로 다시 손보고 수정할 예정 2020-07-18

  • 유저가 로그인 했는데, 저장한 기록이 있으면
<!-- {% if user.is_authenticated %} 위에 작성했으므로 생략 가능 -->
 <div>
   {% for blog in blogs %}
    {% if blog.thumbnail %}
      <!--썸네일이 있으면 띄우기-->
    <img width="600" src="{{ blog.thumbnail.url }}" alt="image" />
    {% endif %}
    <h1>{{ blog.title }}</h1>
    <p>{{blog.pub_date}}</p>
    <p>{{ blog.summary }}</p>
    <a href="{% url 'detail' blog.id %}">더보기</a>
    <hr />
  {% endfor %}
</div>
  • 유저가 로그인을 안했으면
{% else %}
  <a href={% url 'login' %}?next={{request.path}}>login~</a>
  <p>로그인하여 글을 작성해주세요.</p>
{% endif %} <!-- {% if user.is_authenticated %} 를 끝내는 부분 -->

먹기록 삭제

원하는 기록을 삭제

  • 더보기 페이지, 조회 페이지에서 작동

views

def delete(request, blog_id):
    blog_detail = Blog.objects.get(id=blog_id)
    blog_detail.delete()
    return redirect('/blog/')

삭제하고자 하는 기록의 id값을 받아와 해당 객체를 삭제하고, 홈화면으로 redirect 한다.

urls


urlpatterns = [
    path('write', blog.write, name="write"),
    path('blog/<int:blog_id>', blog.detail, name="detail"),
    path('create', blog.create, name="create"),
    path('', blog.tempHome, name="tempHome"),
    path('<int:blog_id>/edit', blog.edit, name="edit"), # 수정하기 url 수정함
    path('<int:blog_id>/delete', blog.delete, name="delete"), # 삭제하기 
]

삭제하려는 기록의 id값을 받아야 하므로 id값을 포함한 url로 만든다.

html

  • tempHome.html
 <div>
   {% for blog in blogs %}
    {% if blog.thumbnail %}
    <img width="600" src="{{ blog.thumbnail.url }}" alt="image" />
    {% endif %}
    <h1>{{ blog.title }}</h1>
    <p>{{blog.pub_date}}</p>
    <p>{{ blog.summary }}</p>
    <a href="{% url 'detail' blog.id %}">더보기</a>
    <a href="{% url 'edit' blog.id %}">수정하기</a> 
    <a href="{% url 'delete' blog.id %}">삭제하기</a>
   <!--조회하는 곳에서 수정, 삭제 다 될 수 있도록 고침 -->
    <hr />
  {% endfor %}
</div>
  • detail.html
{% load static %}
<h1>{{ detail.title }}</h1>
{% if detail.thumbnail %}
<img src="{{detail.thumbnail.url}}" alt="thumbnail" width='600'>
{% else %}
<img src="{% static 'image/default.jpg' %}" alt="thumbnail" width='600'>
{% endif %}
<p> {{ detail.pub_date }} </p>
<p> {{ detail.body }} </p>
<a href="{% url 'edit' detail.id %}">수정하기</a>
<a href="{% url 'delete' detail.id %}">삭제하기</a> <!--삭제하기 추가--> 
<a href="{% url 'tempHome' %}">임시홈으로 돌아가기</a>

delete 뷰를 만들 때, 해당 기록의 id값을 받아오기로 하였으므로 url 뒤에 id 값을 붙여서 작동시킨다.

로그인 상태별 로그인/로그아웃 설정

로그인을 하면 - logout / username이 뜨도록
로그아웃을 하면 - login이 뜨도록

html

{% if user.is_authenticated %}
	<a href={% url 'logout' %}?next={{request.path}}>logout~</a>
	<span>{{request.user}}님</span>
	<!-- (중간 생략) -->
{% else %}
	<a href={% url 'login' %}?next={{request.path}}>login~</a>
	<p>로그인하여 글을 작성해주세요.</p>
{% endif %}
  • 로그인을 하면 logout을 할 수 있는 url이 보이면서 현재 사용자의 username이 보이도록 하였고,
  • 로그인이 안되어있으면 login을 할 수 있는 url이 보이도록 하였다.

(자잘한) 앞으로 해야할 것들

  • 회원 정보 수정
  • 회원 탈퇴
  • 메시지 부분 작동 고치기
profile
개발 공부하는 심리학도

0개의 댓글