이전 페이지 정보 넘기기 : 겟방식으로 이전 페이지 url 날려.
get : 주소 뒤에 정보를 붙여 -> url?name=최유림&gender=female
<form action="url" method="get">
<input>
<input>
post
file 업로드
<input type="file" id="input_file">
<input type="text" id="input_text">
<button type="button" onclick="check_file();">첨부파일확인</button>
<script>
fuction check_file(){
alert(document.getElementById("input_file").value);
}
document.getElementById("input_text").value="하하하";
</script>
---> 파일첨부는 자바스크립트로 통제 X.
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
from django.http import HttpResponseRedirect
from .forms import UploadFileForm
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(req):
if request.method == 'POST':
form = UploadFileForm(req.POST, req.FILES)
if form.is_valid():
handle_uploaded_file(req.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(req, 'upload.html', {'form': form})
def handle_uploaded_file(f):
with open('/root/yook-django/my_homepage/member/some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
#chunk 한줄 한줄 읽는다.
#some/file/name.txt 이거 열어 destination이라고 불러. 그런다음에 지금 업로드한 파일의 내용을 destination에 한줄 한줄 적어라~
import os
os.path.abspath('./member/static/ssac.txt') -> .은 프로젝트 폴더가 기준
#upload.html
<form enctype="multipart/form-data"
method="post">
{% csrf_token %}
{{form}}
<button type="submit">업로드</button>
</form>
from .forms import UploadFileForm
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(req):
if request.method == 'POST':
form = UploadFileForm(req.POST, req.FILES)
if form.is_valid():
handle_uploaded_file(req.FILES['file'])
return render(req, 'main.html')
else:
form = UploadFileForm()
return render(req, 'upload.html', {'form': form})
from .forms import UploadFileForm
def upload_file(req):
if req.method == 'POST':
form = UploadFileForm(req.POST, req.FILES)
if form.is_valid():
ssac_upload(req.FILES['file'])
return render(req, 'main.html')
else:
form = UploadFileForm()
return render(req, 'upload.html', {'form': form})
def ssac_upload(f):
with open( os.path.abspath( './member/static/ssac.txt' ), 'wb+') as dest:
for chunk in f.chunks():
dest.write(chunk)
#views.py
from django import forms
class UploadFileForm(forms.Form):
file = forms.FileField()
#forms.py
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button type="submit">업로드</button>
</form>
#upload.html
#forms.py 안 쓰고
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="my_file">
<button type="submit">업로드</button>
</form>
#upload.html
def upload_file(req):
if req.method == 'POST':
with open( os.path.abspath( './member/static/' + req.FILES['my_file'].name ), 'wb+') as dest:
for chunk in req.FILES['my_file'].chunks():
dest.write(chunk)
return render(req, 'a.html')
else:
return render(req, 'upload.html')
#views.py