๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Django

Django_์žฅ๊ณ  ๋ถ€์ˆ˜๊ธฐ (6)_CRUD

by KASSID 2022. 6. 30.

๋ชฉ์ฐจ

    728x90

    CRUD

    (Create Read Update Delete)

    ์ƒ์„ฑ, ์ฝ๊ธฐ, ์ˆ˜์ •, ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž

     

    Form

    CRUD๋ฅผ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ํƒœ๊ทธ์ด๋‹ค.

    ์ด ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

    ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์–‘์‹์„ ๋งŒ๋“ค์–ด ๋‚ด๊ธฐ๋„ ํ•˜๊ณ , ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.

     

    1. forms.py ์ƒ์„ฑ

    - ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— ํ•ด๋‹น ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

    - form ๋ชจ๋ธ์„ ์žฅ๊ณ ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๊ณ , ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์›ํ•˜๋Š” ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€์•ผํ•œ๋‹ค.

     

    ์˜ˆ์‹œ

    from django import forms
    
    from .models import Post
    
    class PostForm(forms.ModelForm):
    
        class Meta:
            model = Post
            fields = ('title', 'text',)

    - class ํ˜•ํƒœ๋กœ ์ž„์˜์˜ ํผ์„ ๋งŒ๋“ ๋‹ค.(PostForm)

    - 'forms.ModelForm'์„ ์ด์šฉํ•˜์—ฌ ModelForm์ด๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•œ๋‹ค.

    - class Meta๋Š” ํ•ด๋‹น ํผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ค model์ด ์“ฐ์—ฌ์•ผ ํ•˜๋Š”์ง€ ์žฅ๊ณ ์— ์•Œ๋ ค์ฃผ๋Š” ๊ตฌ๋ฌธ์ด๋‹ค.

      ๋˜ํ•œ ์ž…๋ ฅ๋ฐ›์„ ํ•„๋“œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

     

    2. urls.py

    - ํŽธ์ง‘์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€์˜ url์„ ์ƒ์„ฑํ•œ๋‹ค.

     

    3. views.py

    - ์šฉ๋„์— ๋งž๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    - 'form = PostForm()' (์ƒ์„ฑํ•œ ํผ) ์˜ ํ˜•์‹์œผ๋กœ form์„ ํ˜ธ์ถœํ•œ๋‹ค.

     

    4. template

    - formํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๊ณ  {% csrf_token %}์„ ํƒœ๊ทธ์˜ ์ฒซ ์ค„์— ๋„ฃ์–ด์ค€๋‹ค.

    {% extends 'blog/base.html' %}
    
    {% block content %}
        <h1>New post</h1>
        <form method="POST" class="post-form">{% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="save btn btn-default"><i class="fas fa-save"></i></button>
        </form>
    {% endblock %}

     


    Create(์ƒ์„ฑ)

    1. forms.py : ์ƒ๋žต

    2. urls.py

    path('post/new/', views.post_new, name='post_new'),

    - ์ƒ์„ฑ์„ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ url๊ฒฝ๋กœ์— new๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๊ณ  ํ•ด๋‹น viewํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์˜€๋‹ค.

     

    3. views.py

    def post_new(request):
        if request.method == "POST": #๊ธ€์„ ์“ฐ๊ณ  ๋“ฑ๋ก request
            form = PostForm(request.POST)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.published_date = timezone.now()
                post.save()
                return redirect('post_detail', pk=post.pk)
        else: #์ฒ˜์Œ ํ˜ธ์ถœ
            form = PostForm()
        return render(request, 'blog/post_edit.html', {'form': form})

    - request๋Š” GET, POST์š”์ฒญ์ด ์žˆ๋‹ค.

    1) ์ฒซ ํ˜ธ์ถœ

    - ๊ธ€ ์ƒ์„ฑ์„ ์œ„ํ•ด ์ฒ˜์Œ์œผ๋กœ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ์—๋Š” GET์š”์ฒญ์ด๋ฏ€๋กœ else๋ฌธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

    - ๊ธ€ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ ๋น„์–ด์žˆ๋Š” Form๊ณผ ํ•จ๊ป˜ ํŽธ์ง‘url๋กœ ์ด๋™ํ•œ๋‹ค.

     

    2) ๊ธ€์„ ์ž…๋ ฅ๋ฐ›์€ ๋’ค ๋“ฑ๋ก request

    - ๋‘ ๋ฒˆ์งธ ํ˜ธ์ถœ์€ ์ž…๋ ฅ ๋ฐ›์€ ํ›„ DB์— ์ž…๋ ฅ์„ ๋ฐ”๋ผ๋Š” POST์š”์ฒญ์ด๋‹ค.

    - ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” request.POST์— ๋‹ด๊ฒจ์ ธ ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ๊ฐ’์„ Form์— ๋‹ด๊ณ  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

    - ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” post๋ฅผ ์ž„์‹œ ์ €์žฅํ•œ ํ›„, ์œ ์ € ์ •๋ณด, ์ถœํŒ ์‹œ๊ฐ„์„ ์ถ”๊ฐ€๋กœ ๊ฐ์ฒด์— ์ž…๋ ฅํ•œ ๋’ค ์ตœ์ข… ์ €์žฅํ•œ๋‹ค.

    - ์ดํ›„ ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ url๋กœ ์ด๋™ํ•œ๋‹ค.

     

    4. template: ์ƒ๋žต

     


    Update(์ˆ˜์ •)

    1. forms.py: ์ƒ๋žต

    2. urls.py

    path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),

    - ์ˆ˜์ •์„ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ pk๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผํ•˜๊ณ  ํŽธ์ง‘ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ๋‹ค.

     

    3. views.py

    def post_edit(request, pk):
        post = get_object_or_404(Post, pk=pk)
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.published_date = timezone.now()
                post.save()
                return redirect('post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
        return render(request, 'blog/post_edit.html', {'form': form})

    - Create์™€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ๋‹ค.

    - ๋ณ€์ˆ˜ post์— pk๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผํ•œ ๋ชจ๋ธ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.

    - ์ˆ˜์ •์„ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ธฐ์กด ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ์—ฌ์•ผ ํ•œ๋‹ค.

      ์ด๋Š” 'instance=post'๋ฅผ ์ด์šฉํ•˜์—ฌ Form์— ์ „๋‹ฌํ•œ๋‹ค.

     

    4. template: ์ƒ๋žต


    Delete(์‚ญ์ œ)

    1. forms.py: ์ƒ๋žต

    2. urls.py

    path('post/<int:pk>/delete/', views.post_delete, name='post_delete'),

    3. views.py

    def post_delete(request, pk):
        post = get_object_or_404(Post,pk=pk)
        post.delete()
        return redirect('post_list')

    - pk๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๊ณ  delete()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ญ์ œํ•œ๋‹ค.

    - ์‚ญ์ œ ์ดํ›„์—๋Š” ๊ธ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” url๋กœ ์ด๋™ํ•œ๋‹ค.

     

    4. template: ์ƒ๋žต

    ๋Œ“๊ธ€