# 리스트페이지 출력 (list.html)
views
def home(request):
# select * from addresss_address order by name;
items = Address.objects.order_by('name') # Adrress 클래스 . object 전체 셀렉트문
return render(request,'address/list.html', \
{'items':items,'address_count':len(items)}) ## {변수명,값} {key,value}
templates/list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>주소록</h2>
<a href="/">Home</a>
<a href="write">등록</a>
등록된 주소 : {{ address_count }}
<table border="1">
<tr>
<Td>번호</Td>
<Td>이름</Td>
<Td>전화</Td>
<Td>이메일</Td>
<Td>주소</Td>
</tr>
{% for row in items %}
<tr>
<td>{{row.idx}}</td>
<td><a href="detail?idx={{ row.idx }}"> {{ row.name }} </a></td>
<td>{{ row.tel }}]</td>
<td>{{ row.email }}</td>
<Td>{{ row.address }}</Td>
</tr>
{% endfor %}
</table>
</body>
</html>
# 작성페이지 출력 (list.html)
********** {% csrf_token %} 꼭 작성 // 정상적인 경로인지 확인하는 것
viesw.py
def write(request):
return render(request,'address/write.html')
templates/write.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<H2>주소록</H2>
<form method="post" action="insert">
{% csrf_token %}
이름 : <input type="text" name="name"><br>
전화 : <input type="text" name="tel"><br>
메일 : <input type="text" name="email"><br>
주소 : <input type="text" name="address"><br>
이름 : <input type="submit" value="확인"><br>
</form>
</body>
</html>
submit 으로 전송하면
views.py 함수 실행 ( insert )
def insert(request): ## 폼에 입력한 값들이 request 에 저장
# #post방식으로 값전달 POST 방식으로 전달
# request.GET['변수명'] GET 방식으로 전달된 데이터
# Address= > 클래스임
addr=Address(name=request.POST['name'],tel=request.POST['tel'],\
email=request.POST['email'],address=request.POST['address'])
addr.save() # 내부적으로 insert 쿼리 실행 DB 레코드저장
return redirect('/address') # address 주소로 가기
templates/detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function update(){
document.form1.action="update";
document.form1.submit();
}
function del(){
document.form1.action="delete";
document.form1.submit();
}
</script>
</head>
<body>
<H2>주소록 편집</H2>
<form method="post" name="form1">
{% csrf_token %}
<table border="1">
<Tr>
<td>이름</td>
<td><input name="name" value="{{addr.name}}"></td>
</Tr>
<Tr>
<td>전화</td>
<td><input name="tel" value="{{addr.tel}}"></td>
</Tr>
<Tr>
<td>이메일</td>
<td><input name="email" value="{{addr.email}}"></td>
</Tr>
<Tr>
<td>주소</td>
<td><input name="address" value="{{addr.address}}"></td>
</Tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="idx" value="{{ addr.idx }}">
<input type="button" value="수정" onclick="update()">
<input type="button" value="삭제" onclick="del()">
</td>
</tr>
</table>
</form>
</body>
</html>
#address/views
def detail(request):
id=request.GET['idx']
addr = Address.objects.get(idx=id)
return render(request,'address/detail.html',{'addr':addr})
def update(request):
id=request.POST['idx']
addr=Address(idx=id, name=request.POST['name'] ,tel=request.POST['tel'],
email=request.POST['email'],address=request.POST['address'])
addr.save()
return redirect('/address')
def delete(request):
id=request.POST['idx']
Address.objects.get(idx=id).delete()
return redirect('/address')
'기존 > 🏀Pyton' 카테고리의 다른 글
[파이썬-7] 메모장 구현하기 (0) | 2022.08.08 |
---|---|
[파이썬-5] 파이참 urls 패턴 (0) | 2022.08.08 |
[파이썬-4] 파이참 웹서버 구동 ( 웹페이지서버실행 ) (0) | 2022.08.08 |
[파이썬-3] 파이참 애플리케이션생성,테이블생성,SQLITE 생성 (0) | 2022.08.08 |
[파이썬-2] 파이참 설치하기 (0) | 2022.08.06 |