Django
[Django] 사진 CRUD(5) - 사진 추가하기(C)
기록하는_사람
2022. 12. 20. 21:16
사진 추가하기
📌 사진 추가하기
① 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() 사용.
사진 추가하기 - 결과
📌 사진 추가하기 - 결과