URL URI 연결해주기
from django.contrib import admin
from django.urls import path
import board.views
urlpatterns = [
path('admin/', admin.site.urls),
path('board/', board.views.list),
path('board/list', board.views.list),
path('board/register',board.views.register),
path('board/read/<int:bid>', board.views.read),
path('board/update/<int:bid>', board.views.update),
path('board/delete/<int:bid>', board.views.delete),
]
게시판 만들기
새 게시물 작성 함수
def register(request):
if request.method == "GET":
bname = request.GET.get('bname', None)
boardForm = BoardForm()
return render(request,'board/register.html', {'boardForm' : boardForm})
elif request.method =="POST":
bname = request.GET.get('bname', None)
boardForm = BoardForm(request.POST)
if boardForm.is_valid():
board = boardForm.save(commit=False)
board.save()
return redirect('/board/list')
게시판 게시물 리스트 함수
/board/
/board/list
#리스트함수 게시판명별로 정렬추가
def list(request):
bname = request.GET.get('bname', None)
if bname != None: posts = Board.objects.filter(bname=bname).order_by('-id')
else: posts = Board.objects.all().order_by('-id')
return render(request,'board/list.html',{'posts':posts})
게시판 작성글 보기 함수
#read함수
def read(request,bid):
post = Board.objects.get(id=bid)
return render(request,'board/read.html',{'post':post})
게시판 내용수정 함수
def update(request, bid):
print(Board.objects.get)
post = Board.objects.get(Q(id=bid))
if request.method == "GET":
boardForm = BoardForm(instance=post)
return render(request,'board/update.html',{'boardForm':boardForm})
elif request.method == "POST":
boardForm = BoardForm(request.POST)
post = Board.objects.get(Q(id=bid))
if boardForm.is_valid():
post.title = boardForm.cleaned_data['title']
post.contents = boardForm.cleaned_data['contents']
post.password = boardForm.cleaned_data['password']
post.bname = boardForm.cleaned_data['bname']
post.save()
return redirect('/board/read/'+str(bid))
return redirect('/board/list')
게시판 삭제 함수
#삭제함수
def delete(request,bid):
post = Board.objects.get(id=bid)
print(str(bid)+'번게시글 삭제')
post.delete()
return redirect('/board/list')
/board/register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>글작성</title>
</head>
<body>
<form method="post" action="/board/register">
{% csrf_token %}
{{ boardForm }}
<input type="submit" value="작성">
</form>
</body>
</html>
/board/list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>list</title>
</head>
<body>
<table border="1">
<thead>
<th>글번호</th>
<th>제목</th>
<th>게시판명</th>
<th>작성일자</th>
<th>수정</th>
<th>삭제</th>
</thead>
{% if posts %}
{% for post in posts %}
<tr>
<td>{{ post.id }}</td>
<td>{{ post.bname }}</td>
<td><a href="/board/read/{{post.id}}">{{ post.title }}</a></td>
<td>{{ post.create_date }}</td>
<td>
<a href="/board/update/{{post.id}}">수정</a>
</td>
<td>
<a href="/board/delete/{{post.id}}">삭제</a>
</td>
</tr>
{% endfor %}
{% endif %}
</table>
</br>
<a href="/board/register">게시글쓰기</a></br>
</body>
</html>
/board/edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>글수정</title>
</head>
<body>
<form method="post">
{% csrf_token %}
{{ from.as_table }}
<input type="submit" value="작성">
</form>
</body>
</html>
/board/read.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>페이지읽기</title>
</head>
<body>
{% if post %}
{{ post.id }}번게시글 게시판명 {{ post.bname }}</br>
게시글제목: {{ post.title }} 작성일자: {{ post.create_date }} 암호: {{ post.password }}</br>
게시글내용:</br> {{ post.contents }}</br>
{% endif %}
</br></br></br></br></br>
<a href="/board">리스트로 돌아가기</a></br>
</body>
</html>
더보기???? 소스 완전수정
더보기
urls.py
from django.contrib import admin
from django.urls import path
import board.views
urlpatterns = [
path('admin/', admin.site.urls),
path('board/', board.views.list),
path('board/list', board.views.list),
path('board/prog/<str:type>', board.views.prog),
]
view.py
from board.froms import BoardForm # 게시판 폼을 만들어둔걸 불러오기위한 거
from django.shortcuts import redirect
from django.shortcuts import render
from board.models import Board #Board 를 불러오기위한 Board 모델불러오기
from django.db.models import Q
#새글작성함수
#prog 통합함수
def prog(request,type):
bname = request.GET.get('bname', None)
if bname == None : request.POST.get('bname', None)
#조건 타입이 뭔지 보자
if type =='new': #새 게시물 작성 /board/prog/new?bname=free /board/porg/read?id=3
if request.method == "GET":
bname = request.GET.get('bname', None)
html_title = "새 게시물 작성"
boardForm = BoardForm(
initial={
'bname': bname
},
)
return render(request,'board/prog.html', {'boardForm' : boardForm,'html_title':html_title})
elif request.method == "POST":
#bname = request.GET.get('bname', None)
boardForm = BoardForm(request.POST)
if boardForm.is_valid():
board = boardForm.save(commit=False)
board.save()
return redirect('/board/list')
elif type =='edit': # 수정작업
bid = request.GET.get('bid', None)
post = Board.objects.get(Q(id=bid))
if request.method == "GET":
boardForm = BoardForm(instance=post)
return render(request, 'board/prog.html', {'boardForm': boardForm})
elif request.method == "POST":
boardForm = BoardForm(request.POST)
post = Board.objects.get(Q(id=bid))
if boardForm.is_valid():
post.title = boardForm.cleaned_data['title']
post.contents = boardForm.cleaned_data['contents']
post.password = boardForm.cleaned_data['password']
post.bname = boardForm.cleaned_data['bname']
post.save()
return redirect('/board/prog/read?bid='+str(bid)+'&bname='+bname)
return redirect('/board/list')
elif type == 'read': # 게시글 읽어와서 prog.html에 뿌려주기
print("read")
bid = request.GET.get('bid', None)
post = Board.objects.get(id=bid)
return render(request, 'board/prog.html', {'post': post})
elif type == 'del':
bid = request.GET.get('bid', None)
post = Board.objects.get(id=bid)
print(str(bid)+'번게시글 삭제')
post.delete()
return redirect('/board/list')
else:
print("타입없음에러")
return render(request, 'board/prog.html')
#리스트함수 게시판명별로 정렬추가
def list(request):
bname = request.GET.get('bname', None)
if bname != None: posts = Board.objects.filter(bname=bname).order_by('-id')
else: posts = Board.objects.all().order_by('-id')
return render(request,'board/list.html',{'posts':posts})
list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>list</title>
</head>
<body>
<table border="1">
<thead>
<th>글번호</th>
<th>제목</th>
<th>게시판명</th>
<th>작성일자</th>
<th>수정</th>
<th>삭제</th>
</thead>
{% if posts %}
{% for post in posts %}
<tr>
<td>{{ post.id }}</td>
<td>{{ post.bname }}</td>
<td><a href="/board/prog/read?bid={{post.id}}&bname={{post.bname}}">{{ post.title }}</a></td>
<td>{{ post.create_date }}</td>
<td>
<a href="/board/prog/edit?bid={{post.id}}&bname={{post.bname}}">수정</a>
</td>
<td>
<a href="/board/prog/del?bid={{post.id}}&bname={{post.bname}}">삭제</a>
</td>
</tr>
{% endfor %}
{% endif %}
</table>
</br>
<a href="/board/prog/new?bname=free">free게시글쓰기</a></br>
</body>
</html>
prog.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{html_title}}</title>
</head>
<body>
{% if post %}
{{ post.id }}번게시글 게시판명 {{ post.bname }}</br>
게시글제목: {{ post.title }} 작성일자: {{ post.create_date }} 암호: {{ post.password }}</br>
게시글내용:</br> {{ post.contents }}</br>
</br></br></br></br></br>
<a href="/board">리스트로 돌아가기</a></br>
{% else %}
<form method="post">
{% csrf_token %}
{{ boardForm.as_p }}
<input type="submit" value="작성">
</form>
{% endif %}
</body>
</html>
728x90
반응형