from django.shortcuts import redirect, render, get_object_or_404
from .models import Article
from .forms import ArticleForm
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods, require_POST
# Create your views here.
@login_required
def create(request):
if request.method == "POST":
form = ArticleForm(request.POST, request.FILES)
if form.is_valid():
article = form.save(commit=False)
article.author = request.user
article.save()
return redirect("articles:article_detail", article.pk)
else:
form = ArticleForm()
context = {"form": form}
return render(request, "products/create.html", context)
def article_detail(request, pk):
article = get_object_or_404(Article, pk=pk)
context = {
"article": article,
}
return render(request, "products/product_detail.html", context)
def articles(request):
articles = Article.objects.all().order_by("-pk")
context = {
"articles": articles,
}
return render(request, "products/products.html", context)
@login_required
@require_http_methods(["GET", "POST"])
def update(request, pk):
article = get_object_or_404(Article, pk=pk)
if request.method == "POST":
form = ArticleForm(request.POST, instance=article)
if form.is_valid():
article = form.save()
return redirect("articles:article_detail", article.pk)
else:
form = ArticleForm(instance=article)
context = {
"form": form,
"article": article,
}
return render(request, "products/update.html", context)
@require_POST
def delete(request, pk):
article = get_object_or_404(Article, pk=pk)
if request.user.is_authenticated:
if article.author == request.user:
article = get_object_or_404(Article, pk=pk)
article.delete()
return redirect("articles:articles")
@require_POST
def like(request, pk):
if request.user.is_authenticated:
article = get_object_or_404(Article, pk=pk)
if article.like_users.filter(pk=request.user.pk).exists():
article.like_users.remove(request.user)
else:
article.like_users.add(request.user)
return redirect("articles:article_detail", pk=pk)
return redirect("accounts:login")
게시글에 사진까지 첨부되도록 구현
오늘의 회고
오전중으로 기능구현 마무리
결과보고서 초반 작성 완료
'매일 TIL' 카테고리의 다른 글
[내일배움캠프 10-2일] 팀프로젝트 최종 마무리, 제출 서류 작성 (0) | 2024.08.28 |
---|---|
[내일배움캠프 10-1일] 팀프로젝트 부트스트랩 적용 (0) | 2024.08.26 |
[내일배움캠프 9-4일] 팀프로젝트 프로필 사진 구현 (0) | 2024.08.22 |
[내일배움캠프 9-3일] 팀프로젝트 회원가입, 탈퇴, 수정, 유저 프로필 구현 (0) | 2024.08.21 |
[내일배움캠프 9-2일] 팀프로젝트 로그인 기능 구현 (0) | 2024.08.20 |