개인프로젝트 진행(3)

·2022년 11월 30일
0

TIL

목록 보기
43/46

마크다은

게시글, 댓글에도 마크다운 작성 가능

마크다운 설치
pip install markdown

\main\templatetags\main_filter.py

from django import template
import markdown
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter
def sub(value, arg):
    return value - arg

@register.filter
def mark(value):
    extensions = ["nl2br", "fenced_code"]
    return mark_safe(markdown.markdown(value, extensions=extensions))

main\question_detail.html

{% extends 'base.html' %}
{% load main_filter %}
{% block content %}
<div class="container my-3">
    <!-- message 표시 -->
    {% if messages %}
    <div class="alert alert-danger my-3" role="alert">
    {% for message in messages %}
        <strong>{{ message.tags }}</strong>
        <ul><li>{{ message.message }}</li></ul>
    {% endfor %}
    </div>
    {% endif %}
    <h2 class="border-bottom py-2">{{ question.subject }}</h2>
    <div class="card my-3">
        <div class="card-body">
            <div class="card-text" >{{ question.content|mark }}</div>
            <div class="d-flex justify-content-end">

댓글 수정

templates\main\question_detail.html

<div class="d-flex justify-content-end">
            <div class="badge bg-light text-dark p-2 text-start">
                <div class="mb-2">{{ answer.author.username }}</div>
                <div>{{ answer.create_date }}</div>
            </div>
        </div>
        <div class="my-3">
            {% if request.user == answer.author %}
            <a href="{% url 'main:answer_modify' answer.id  %}" 
               class="btn btn-sm btn-outline-secondary">수정</a>
            {% endif %}
        </div>
    </div>
</div>
{% endfor %}

urls.py

urlpatterns = [

    path('answer/modify/<int:answer_id>/', views.answer_modify, name='answer_modify'),
]

views.py

from .models import Question, Answer


@login_required(login_url='common:login')
def answer_modify(request, answer_id):
    answer = get_object_or_404(Answer, pk=answer_id)
    if request.user != answer.author:
        messages.error(request, '수정권한이 없습니다')
        return redirect('main:detail', question_id=answer.question.id)
    if request.method == "POST":
        form = AnswerForm(request.POST, instance=answer)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.modify_date = timezone.now()
            answer.save()
            return redirect('main:detail', question_id=answer.question.id)
    else:
        form = AnswerForm(instance=answer)
    context = {'answer': answer, 'form': form}
    return render(request, 'main/answer_form.html', context)

댓글 삭제

urls.py

urlpatterns = [
    
    path('answer/delete/<int:answer_id>/', views.answer_delete, name='answer_delete'),
]

views.py

@login_required(login_url='common:login')
def answer_delete(request, answer_id):
    answer = get_object_or_404(Answer, pk=answer_id)
    if request.user != answer.author:
        messages.error(request, '삭제권한이 없습니다')
    else:
        answer.delete()
    return redirect('main:detail', question_id=answer.question.id)

0개의 댓글