django 3주 숙제

Grace Goh·2022년 10월 11일
0

Django

목록 보기
19/32

3주 목표

  • 사용자모델 확장 (상속)
  • 회원가입, 로그인, 로그아웃
  • 게시글 작성, 삭제, 읽기

숙제

힌트를 참고해서 댓글쓰기 삭제 불러오기 기능 만들기
(모델링 후 makemigrations, migrate 하기)

tweet/models.py

from django.db import models
from user.models import UserModel


# Create your models here.
class TweetModel(models.Model):
    class Meta:
        db_table = "tweet"

    author = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    content = models.CharField(max_length=256)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


class TweetComment(models.Model):
    class Meta:
        db_table = "comment"
    tweet = models.ForeignKey(TweetModel, on_delete=models.CASCADE)
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    comment = models.CharField(max_length=256)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
python manage.py makemigrations
python manage.py migrate

tweet/views.py


@login_required
def detail_tweet(request, id):
    my_tweet = TweetModel.objects.get(id=id)
    tweet_comment = TweetComment.objects.filter(tweet_id=id).order_by('-created_at')
    return render(request,'tweet/tweet_detail.html',{'tweet':my_tweet,'comment':tweet_comment})


@login_required
def write_comment(request, id):
    if request.method == 'POST':
        comment = request.POST.get("comment","")
        current_tweet = TweetModel.objects.get(id=id)

        TC = TweetComment()
        TC.comment = comment
        TC.author = request.user
        TC.tweet = current_tweet
        TC.save()

        return redirect('/tweet/'+str(id))


@login_required
def delete_comment(request, id):
    comment = TweetComment.objects.get(id=id)
    current_tweet = comment.tweet.id
    comment.delete()
    return redirect('/tweet/'+str(current_tweet))

tweet/urls.py


# tweet/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('tweet/', views.tweet, name='tweet'),
    path('tweet/delete/<int:id>', views.delete_tweet, name='delete-tweet'),
    path('tweet/<int:id>', views.detail_tweet, name='detail-tweet'),
    path('tweet/comment/<int:id>', views.write_comment, name='write-comment'),
    path('tweet/comment/delete/<int:id>', views.delete_comment, name='delete-comment'),
]

home.html

<!-- templates/tweet/home.html -->
{% extends 'base.html' %}
{% block title %}
    메인페이지
{% endblock %}

{% block content %}
    <div class="container timeline-container">
        <div class="row">
            <!-- 왼쪽 컬럼 -->
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">{{ user.username }}</h5>
                        <p class="card-text">{{ user.bio }}</p>

                    </div>
                </div>
            </div>
            <!-- 오른 쪽 컬럼-->
            <div class="col-md-7">
                <!-- 글을 작성 하는 곳 -->
                <div class="row mb-2">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="media">
                                    <div class="media-body">
                                        <h5 class="mt-0">나의 이야기를 적어주세요</h5>
                                        <p>
                                        <form action="/tweet/" method="post">
                                            {% csrf_token %}
                                            <div class="form-group mb-2">
                                                <textarea class="form-control" style="resize: none" name='my-content'
                                                          id="my-content"></textarea>
                                            </div>
                                            <button type="submit" class="btn btn-primary" style="float:right;">작성하기
                                            </button>
                                        </form>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <!-- 작성 된 글이 나오는 곳 -->
                <div class="row">
                    {% for tw in tweet %}
                        <div class="col-md-12 mb-2">
                            <div class="card">
                                <div class="card-body">
                                    {% if tw.author == user %}
                                        <div style="text-align: right">
                                            <a href="/tweet/delete/{{ tw.id }}">
                                                <span class="badge rounded-pill bg-danger">삭제</span>
                                            </a>
                                        </div>
                                    {% endif %}
                                    <div style="text-align: right">
                                        <a href="/tweet/{{ tw.id }}">
                                            <span class="badge rounded-pill bg-success">보기</span>
                                        </a>
                                    </div>
                                    <div class="media">
                                        <div class="media-body">
                                            <h5 class="mt-0">{{ tw.content }}</h5>
                                        </div>
                                        <div style="text-align: right">
                                            <span style="font-size: small">{{ tw.author.username }}-{{ tw.created_at|timesince }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>

            </div>
            <div class="col-md-2"></div>
        </div>
    </div>
{% endblock %}
profile
Español, Inglés, Coreano y Python

0개의 댓글