-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguide.txt
More file actions
191 lines (144 loc) · 5.34 KB
/
guide.txt
File metadata and controls
191 lines (144 loc) · 5.34 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
1) 가상환경 실행
.venv/Script/activate.bat
pip install django
2) 프로젝트 생성
django-admin startproject 프로젝트명 .
3) 장고 서버 실행 (django_2025 디렉토리 밑에서)
python manage.py runserver
http://127.0.0.1:8000 확인
장고서버 종료 - cmder에서 ctrl+c
4) 장고 서버 data migration
python manage.py migrate
5) super user 생성 - admin 페이지
python manage.py createsuperuser
5.1 ) 서버 실행
python manage.py runserver
http://127.0.0.1:8000/admin/
6) .gitignore에 올리지 않을것 적기
.venv/
.idea/
db.sqlite3
7) git
git add .
git commit -m "장고프로젝트생성"
git push
1. blog application 생성
- 글 : model : Post
- 글 리스트, 글 개별 페이지
2. library application 생성
- 책: Book
- 책 리스트, 책 개별 페이지
3. shopping mall application 생성
- 상품: Product
- 상품 리스트, 상품 개별 페이지
새로운 application을 생성한다.
1. library, shopping mall
2. 동일한 작업 진행
application에는 기본 생성이 안되어 있음.
blog/urls.py 생성
start bootstrap - templates - blog
blog home , blog post
https://github.com/dongmisw/django_2025
<blog>
http://127.0.0.1:8000/blog
카테고리별리스트 GET /blog/category/<str:slug> ->views.category ->index.html
전체글 보기 GET /blog/ views.index -> index.html
상세글 보기 GET /blog/{pk}/ views.detail -> detail.html
글 삭제하기 GET /blog/{pk}/delete/ views.delete ->글목록
글 업데이트 GET /blog/{pk}/update/ views.update -> postupdateform.html
POST /blog/{pk}/update/ views.update
1) 정상 -> database 저장 -> redirect 전체글리스트
2) 비정상 -> 다시써. -> postupdateform.html
글쓰기 GET /blog/create views.create -> postform.html
완전히 칸이 비어있는 작성화면
글쓰기 제출(form 정보를 장고 서버로 보냄)
POST blog/create views.create
1) 정상 제출인 경우 database저장 -> 게시판 리스트 redirect
2) valid하지 않은 경우() -> 다시써. -> postform.html
ㄴ처음에 글쓰기를 누르면, 비어있는 form이 나옴
ㄴ제대로 안쓰면 다시 쓰라고 하면서 반쯤 채워져있는 form
ㄴ정상적으로 글을 다 쓰면 저장함
-> 전체 글 리스트로 넘어감
template 문법
0) views의 함수가 넘겨준 context 변수 활용
{{ post }}
1) {% load static %}
2)for 문
{% for post in posts %}
작업
{% endfor %}
3) if/else 문
{% if 조건 %}
{% else %}
{% endif %}
4) url
{% url 'bloglist' %}
path('/blog/',
template_name='blog/index.html',
name = 'bloglist')
5) 다른 html에서 확장하기
{% extends 'blog/base.html' %}
-> {% block main-area %}
6) block 대체
{% block main-area %}
,,,,,,
{% endblock %}
7) 다른 html 포함하기
{% include 'blog/footer.html' %}
<django에서 database 접근하기>
python manage.py shell
pip install django_extensions
python manage.py shell_plus
from blog.models import Post
Post.objects.all() -> 전체 가져오기
# 조건을 주어 가져오기
Post.objects.filter(pk=1)
# 조건을 주어 가져온 후에 삭제하기
post = Post.objects.filter(title="1번포스트제목")
post.delete()
# 새로 입력하기,저장하기
post = Post()
post.title="shell에서 만든 post"
post.content = "shell에서 만든 post의 content"
post.author = User.objects.get(username='user3')
post.category = Category.objects.get(name='맛집')
post.uploaded_image = 'images/cat.jpg'
post.save()
# 특정 post를 업데이트하기
1. 조건으로 찾아온다. ->
2. 해당하는 content를 수정
3. save
comment에 대한 crud
1. urls 작성할지 고민
기능 method urls 처리 함수 template이름
댓글 리스트보기 GET /blog/{post.pk} views.detail blog/detail.html
context에 댓글 전체 리스트를 담아서 template으로 보낸다.
댓글 작성하기 POST /blog/{post.pk}/createcomment/ views.createcomment
redirect(/blog/{post.pk})
댓글 수정하기 GET (수정하기버튼)
/blog/{comment.pk}/updatecomment/ views.updatecomment
blog/comment_update_form.html
POST (다 수정한 후에 또 제출하기 버튼)
1) 정상 valid save해라. redirect(/blog/{post.pk})
2) 비정상 valid 안한 경우
다시써. return render(request,
template_name=blog/comment_update_form.html,
context={'form':form})
댓글 삭제하기 GET /blog/{comment.pk}/deletecomment
view.deletecomment
redirect(/blog/{post.pk})
one-to-many 관계
class Comment - author, post 변수
class Post - comment 변수 없음
{% if post.comment_set.exist %}
이 post에 연결된 comment가 있다.
{% else %}
이 post에 연결된 comment가 없다.
{% endif %}
{% for comment in post.comment_set.all %}
{{comment.content}}
{% endfor %}
many-to-many 관계
{% for tag in post.tags.all %}
tag-name : {{ tag.name }}
{% endfor %}