Tags
- update
- Django
- stack
- migrations
- 완전검색
- Article & User
- N:1
- Queue
- 쟝고
- 통계학
- drf
- outer join
- regexp
- distinct
- 뷰
- 그리디
- create
- 스택
- DB
- M:N
- 이진트리
- SQL
- count
- 백트래킹
- delete
- ORM
- Vue
- Tree
- 트리
- 큐
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Notice
Recent Posts
Link
데이터 분석 기술 블로그
Django에 대하여(15)_ORM with view(Create) 본문
1. Create
Create 로직을 구현하기 위해 필요한 view 함수의 개수는 몇 개일까요? 사용자 입력 데이터를 받을 페이지를 렌더링 하는 new와 사용자가 입력한 데이터를 받아 DB에 저장하는 create, 이렇게 두 가지가 있습니다.
1-1 new 기능 구현
# articles/urls.py
urlpatterns = [
...
path('new/', views.new, name='new')
]
# articles/views.py
def new(request):
return render(request, 'articles/new.html')
<!-- template/articles/new.html -->
<h1>NEW</h1>
<form action="#" method="GET">
<div>
<label for="title">Title: </label>
<input type="text" name="title" id="title>
</div>
<label for "content">Content: </label>
<textarea name="content" id="content"></textarea>
</div>
<input type="submit">
</form>
<hr>
<a href="{% url 'articles:index' %}">[back]</a>
<!-- templates/articles/index.html -->
<h1>Articles</h1>
<a href="{% url 'articles:new' %}">NEW</a>
<hr>
...
1-2 create 기능 구현
# articles/urls.py
urlpatterns = [
...
path('create/', views.create, name='create')
]
# articles/views.py
def create(request):
title = request.GET.get('title')
content = request.GET.get('content')
# 1.
# article = Article()
# article.title = title
# article.content = content
# article.save()
# 2.
articles = Article(title=title, content=content)
article.save()
# 3.
# Article.objects.create(title=title, content=content)
return render(request, 'articles/create.html')
<!-- templates/articles/create.html -->
<h1>게시글이 작성 되었습니다.</h1>
<!-- template/articles/new.html -->
<h1>NEW</h1>
<form action="{% url 'articles:create' %}" method="GET">
<div>
<label for="title">Title: </label>
<input type="text" name="title" id="title>
</div>
<label for "content">Content: </label>
<textarea name="content" id="content"></textarea>
</div>
<input type="submit">
</form>
<hr>
<a href="{% url 'articles:index' %}">[back]</a>
'백엔드' 카테고리의 다른 글
Django에 대하여(17)_HTTP request methods와 redirect (0) | 2024.04.07 |
---|---|
Django에 대하여(16)_ORM with view(Delete와 Update) (0) | 2024.04.06 |
Django에 대하여(14)_ORM with view(Read) (0) | 2024.04.04 |
Django에 대하여(13)_QuerySet API 실습 (0) | 2024.04.03 |
Django에 대하여(12)_ORM과 QuerySet API (0) | 2024.04.02 |