반응형
from django.shortcuts import render, redirect
from django.core.paginator import Paginator
from django.http import Http404
from fcuser.models import Fcuser
from tag.models import Tag
from .models import Board
from .forms import BoardForm
# Create your views here.
def board_detail(request, pk):
try:
board = Board.objects.get(pk=pk)
except Board.DoesNotExist:
raise Http404('게시글을 찾을 수 없습니다.')
return render(request, 'board_detail.html', {'board': board})
def board_write(request):
# 로그인을 안했을시 로그인페이지로
if not request.session.get('user'):
return redirect('/fcuser/login/')
if request.method == 'POST':
form = BoardForm(request.POST)
if form.is_valid():
user_id = request.session.get('user')
fcuser = Fcuser.objects.get(pk=user_id)
tags = form.cleaned_data['tags'].split(',')
board = Board()
board.title = form.cleaned_data['title']
board.contents = form.cleaned_data['contents']
board.writer = fcuser
board.save()
for tag in tags:
if not tag:
continue
_tag, _ = Tag.objects.get_or_create(
name=tag) # 태그가 있으면 가져오고 없으면 생성
board.tags.add(_tag)
return redirect('/board/list/')
else:
form = BoardForm()
return render(request, 'board_write.html', {'form': form})
def board_list(request):
all_boards = Board.objects.all().order_by('-id') # -id 최신거를 먼저(시간역순)
page = int(request.GET.get('p', 1))
paginator = Paginator(all_boards, 2) # 화면 기준
boards = paginator.get_page(page)
return render(request, 'board_list.html', {'boards': boards})
반응형
'Python_WEB > Project-Noticeboard' 카테고리의 다른 글
[Django]User > Forms (0) | 2019.12.14 |
---|---|
[Django]Board > Templates > HTML > Login (0) | 2019.12.14 |
[Django]User > Templates > HTML > Register (0) | 2019.12.14 |
[Django]User > Templates > HTML > Home (0) | 2019.12.14 |
[Django]User > Templates > HTML > Basic (0) | 2019.12.14 |