사진 추가하기
📌 사진 추가하기
① index.html에 생성 버튼 추가.
(index.html)
<h1>INDEX PAGE</h1>
{% for i in pset %}
<a href="{% url 'delete' i.id %}"><img src="{{ i.pic.url }}" height="150px"></a>
{% endfor %}
<hr>
<h2>CREATE</h2>
<form action="{% url 'create' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input name="pname" type="text" placeholder="제목"><br><br>
<textarea name="pcont" cols="70" rows="7" placeholder="내용"></textarea><br><br>
<input name="ppic" type="file"><br><br>
<button>CREATE</button>
</form>
<style>
img:hover {
opacity: 0.4;
}
</style>
② (gal/urls.py)
from django.urls import path
from . import views
urlpatterns = [
path('index/', views.index, name="index"),
path('delete/<gpk>', views.delete, name="delete"),
path('create/', views.create, name="create")
]
③ (gal/views.py)
...
def create(request):
pn = request.POST.get("pname")
pc = request.POST.get("pcont")
pp = request.FILES.get("ppic")
Pic(name=pn, content=pc, pic=pp).save()
return redirect("index")
💡 사진 데이터 받을 때는 request.FILES.get() 사용.
사진 추가하기 - 결과
📌 사진 추가하기 - 결과
'Django' 카테고리의 다른 글
[Django] user CRUD(1) - 기본 세팅 (0) | 2022.12.20 |
---|---|
[Django] 사진 CRUD(6) - 사진 없는 경우, 다른 이미지 띄우기 (0) | 2022.12.20 |
[Django] 사진 CRUD(4) - 사진 클릭 시, 삭제하기(D) (0) | 2022.12.20 |
[Django] 사진 CRUD(3) - DB 읽고 화면에 출력하기(R) (0) | 2022.12.20 |
[Django] 사진 CRUD(2) - 사진 관련 설정과 DB 생성 및 레코드 추가 (0) | 2022.12.20 |
댓글