hyeonga_code

파이선 웹구축_장고_24_댓글 선택 삭제 본문

Python_Django

파이선 웹구축_장고_24_댓글 선택 삭제

hyeonga 2023. 6. 16. 05:59
반응형

- 댓글 선택 삭제

1. 상세페이지에 삭제 기능 작성

'detail.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<h1><b>{{ b.subject }}</b></h1>
<h4>written by <b>{{b.writer}}</b></h4>
 
<textarea cols="60" rows="6" disabled>{{ b.content }}</textarea><br><br>
 
<a href="{% url 'index' %}"><button></button></a>
 
<hr>
    <form method='post' action = "{% url 'creply' b.id %}">
        {% csrf_token %}
        <input name="rep" type='text' size="7" placeholder='Input Replyer'>
        <input name='com' type='text' size='30' placeholder='Input Comment'>
        <button>댓글 등록</button>
    </form>
<hr>
{% for i in rset %}
    <b>{{ i.replyer }}</b> {{ i.comment }} <br>
    <a href='{% url 'dreply' b.id i.id%}'><button>X</button></a>
    #_ 인자가 두개 넘어가야 함
{% empty %}
    <b>댓글</b>을 달아주세요
{% endfor %}
cs

 

 

 

 

2. 경로 설정

'urls.py'

=====

1
2
3
4
5
6
7
8
9
10
from django.urls import path
from . import views
 
urlpatterns = [
    path('index/', views.index, name="index"),
    path('detail/<bpk>', views.detail, name="detail"),
    path('delete/<bpk>', views.delete, name="delete"),
    path('creply/<bpk>', views.creply, name='creply'),
    path('dreply/<bpk>/<rpk/', views.dreply, name='dreply'),
]
cs

 

 

 

3. 함수 설정

'views.py'

=====

1
2
3
def  dreply(request, bpk, rpk):
    Reply.objects.get(id=rpk).delete()
    return redirect("detail", bpk)
cs

 

 

= 장고 폴더 안에 여러 앱이 있을 경우, 별칭의 중복 오류가 생길 수 있으므로 주의해야 한다.

> <acc> > 'urls.py'

=====

1
app_name = "acc"
cs

 

 

>> 모든 파일의 별칭 변경해주기!

 

 

반응형