728x90
CRUD ๋?
- Create : DB์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํจ
- Read : DB์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ด
- Update : DB์ ํน์ ๋ฐ์ดํฐ๋ฅผ ์์ ํจ
- Delete : DB์ ํน์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํจ
Create - ๋ฐ์ดํฐ ์์ฑ
- GET : ๋ฐ์ดํฐ๋ฅผ ์ป๊ธฐ ์ํ ์์ฒญ
- ๋ฐ์ดํฐ๊ฐ URL์ ๋ํ๋จ
# ex) ๊ฒ์ ๊ธฐ๋ฅ search
def search(request):
keyword = request.GET['search_value']
- POST : ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๊ธฐ ์ํ ์์ฒญ
- ๋ฐ์ดํฐ๊ฐ URL์ ๋ํ๋์ง ์์
- csrf ๊ณต๊ฒฉ ๋ฐฉ์ง ํ์
# views.py
def create(request):
new_blog = Blog() # ๋ฐ์ดํฐ ์ ์ฅ์ ์ํ ๊ฐ์ฒด ์์ฑ
new_blog.title = request.POST['title']
new_blog.writer = request.POST['writer']
new_blog.body = request.POST['body']
new_blog.pub_date = timezone.now()
new_blog.save() # ๊ฐ์ฒด ์ ์ฅ
return redirect('detail', new_blog.id)
<!-- new.html -->
<form action="{% url 'create' %}" method="post">
{% csrf_token %}
<input type="text" name="title">
<input type="text" name="writer">
<textarea name="body"></textarea>
<button type="submit">์ ์ถํ๊ธฐ</button>
</form>
- form ์ฌ์ฉ : ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ์ฉ์ด
# form.py ์์ฑ
from django import forms
from .models import Blog
class BlogForm(forms.ModelForm):
class Meta:
model = Blog
fields = ['title','writer','body']
# views.py
def new(request):
form = BlogForm() # ํผ ์์ฑ
return render(request, 'new.html', {'form' : form})
def create(request):
form = BlogForm(request.POST, request.FILES)
if form.is_valid():
new_blog = form.save(commit=False)
new_blog.pub_date = timezone.now()
new_blog.save()
return redirect('detail' , new_blog.id)
return redirect('home')
<!-- new.html -->
<form action="{% url 'create' %}" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">์ ์ถํ๊ธฐ</button>
Read - ๋ฐ์ดํฐ ํธ์ถ
- ์ฟผ๋ฆฌ์ : ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ชจ๋ ํ๋ ๊ฐ์ ธ์ค๊ธฐ
# views.py
def blogList(request):
blogs = Blog.objects.all() # 1) ๋ชจ๋ ๊ฐ์ฒด
blogs = Blog.objects.filter(writer='CJY') # 2) writer='CJY'๋ฅผ ๋ง์กฑํ๋ ๋ชจ๋ ๊ฐ์ฒด
return render(request, 'blogList.html', {'blogs': blogs})
<!-- blogList.html -->
{% for blog in blogs %} <!--for ๋ฌธ์ ํตํ ๊ฐ์ฒด ์ ๊ทผ-->
{{blog.title}}
{{blog.body}}
{% endfor %}
- ๊ฐ์ฒด : ์กฐ๊ฑด์ ๋ถํฉํ๋ ํ๋ ํ๋ ๊ฐ์ ธ์ค๊ธฐ
def detail(request, blog_id):
details = Blog.objects.get(pk=blog_id) # 1) pk=blog_id๋ฅผ ๋ง์กฑํ๋ ๊ฐ์ฒด ํ๋
details = get_object_or_404(Blog, pk=blog_id) # 2) ์๋ชป๋ ์ ๊ทผ์ ๋ํ ์์ธ์ฒ๋ฆฌ
return render(request, 'detail.html', {'details': details})
<!-- datail.html -->
{{details.title}} <!--๊ฐ์ฒด ๋ฐ๋ก ์ ๊ทผ-->
{{details.body}}
Update - ๋ฐ์ดํฐ ์์
- ํด๋น ๊ฐ์ฒด์ ์ ๊ทผํ์ฌ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ ํ ์์ ํ์ฌ ์ ์ฅ
# views.py
def update(request, id):
update_blog = Blog.objects.get(id=id) # ๊ธฐ์กด ๋ฐ์ดํฐ ๋ก๋
update_blog.title = request.POST['title']
update_blog.writer = request.POST['writer']
update_blog.body = request.POST['body']
update_blog.pub_date = timezone.now()
update_blog.save() # ์๋ก ์ ์ฅ
return redirect('detail' , update_blog.id)
<!-- edit.html -->
<form action="{% url 'update' blog.id %}" method="post">
{% csrf_token %}
<input type="text" name="title" value="{{blog.title}}" />
<input type="text" name="writer" value="{{blog.writer}}" />
<textarea name="body">{{blog.body}}</textarea>
<button type="submit">์ ์ถํ๊ธฐ</button>
</form>
Delete - ๋ฐ์ดํฐ ์ญ์
- ํด๋น ๊ฐ์ฒด์ ์ ๊ทผํ์ฌ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ ํ ์ญ์
# views.py
def delete(request, id):
delete_blog = Blog.objects.get(id = id)
delete_blog.delete()
return redirect('blogList')
<!-- detail.html -->
<a href="{% url 'delete' details.id%}" role="button">์ญ์ ํ๊ธฐ</a>
728x90
'Backend > ๐ต Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Django] ์ฅ๊ณ ์ ์ ํ์ผ ๊ด๋ฆฌํ๊ธฐ - Static, Media (0) | 2021.07.07 |
---|---|
[Django] ์ฅ๊ณ models ๋ฐ DB ์ค๊ณ - Migration, Admin (1) | 2021.05.16 |
[Django] ์ฅ๊ณ ์ค์ต/๊ฐ๋ฐ ํ๊ฒฝ ์ค๋นํ๊ธฐ - ํ๋ก์ ํธ ๋ฐ ์ฑ ์์ฑ (0) | 2021.05.11 |