WEB/Django prac

[Django prac][ORM with View] CRUD 구현 6

bay07 2024. 3. 27. 16:52

 

게시글 작성하는 페이지가 존재하고,

거기서 작성했을 때 

게시글이 써져서 메인 페이지에 출력되도록 해야한다. 

 

게시글 작성이라는 하나의 cycle 로직을 만들기 위해서는

view 함수가 2개 필요하다 

throw, catch랑 비슷하다 

new create
사용자의 입력 데이터를 받을
페이지를 렌더링한다. 

사용자가 입력한 데이터를 받아서 
DB에 저장한다

"데이터를 입력하세요 :     " "DB야 이 데이터 저장해~"

 


# articles > ulrs.py

더보기
from django.urls import path
from . import views

app_name = 'articles'
urlpatterns = [
    # ''는 메인페이지를 의미한다.
    path('', views.index, name='index'),
    path('<int:pk>/', views.detail, name='detail'),
    path('new/', views.new, name='new'),
]

# articles > veiws.py 

더보기
from django.shortcuts import render
from .models import Article

def index(request):
    articles = Article.objects.all()
    context = {
        'articles' : articles,
    }
    return render(request, 'articles/index.html', context)

def detail(request, pk):
    article = Article.objects.get(pk=pk)
    context = {
        'article': article,
    }
    return render(request, 'articles/detail.html', context)

def new(request):
    return render(request, 'articles/new.html')