Skip to content

Commit 2b1577d

Browse files
authored
Merge pull request #181 from kewtree1408/fix-publish-button-section
Fix "Add publish button" and "Delete post" sections
2 parents 899ab34 + d6d2b38 commit 2b1577d

File tree

6 files changed

+71
-39
lines changed

6 files changed

+71
-39
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Current tutorials are:
2020
## Contributing
2121

2222
These tutorials are maintained by [DjangoGirls](http://djangogirls.org/). If you find any mistakes or want to update the tutorial
23-
please [follow the contributing guidelines](https://github.com/DjangoGirls/tutorial-extension/contributing/).
23+
please [follow the contributing guidelines](./contributing/README.md).

en/homework/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Time to do something similar, but for draft posts.
2121
Let's add a link in `blog/templates/blog/base.html` in the header. We don't want to show our list of drafts to everybody, so we'll put it inside the {% raw %}`{% if user.is_authenticated %}`{% endraw %} check, right after the button for adding new posts.
2222

2323
```django
24-
<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
24+
<a href="{% url 'post_draft_list' %}" class="top-menu">Drafts</span></a>
2525
```
2626

2727
Next: urls! In `blog/urls.py` we add:
@@ -84,8 +84,8 @@ into these:
8484
{{ post.published_date }}
8585
</div>
8686
{% else %}
87-
<form method="POST" action="{% url post_publish pk=post.pk %} class="post-form">{% csrf_token %}
88-
<button type="submit" class="post btn btn-info" name="publish">Publish</button>
87+
<form method="POST" action="{% url 'post_publish' pk=post.pk %}" class="post-form">{% csrf_token %}
88+
<button type="submit" class="post btn btn-secondary" name="publish">Publish</button>
8989
</form>
9090
{% endif %}
9191
```
@@ -101,7 +101,7 @@ Now, let's take a look at the details of the form. We are using a new attribute,
101101
Time to create a URL (in `blog/urls.py`):
102102

103103
```python
104-
path('post/<pk>/publish/', views.post_publish, name='post_publish'),
104+
path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
105105
```
106106

107107
and finally, a *view* (as always, in `blog/views.py`):
@@ -137,9 +137,9 @@ Congratulations! You are almost there. The last step is adding a delete button!
137137
Let's open `blog/templates/blog/post_detail.html` once again and add these lines:
138138

139139
```django
140-
<form method="POST" action="{% url post_remove pk=post.pk %} class="post-form">{% csrf_token %}
140+
<form method="POST" action="{% url 'post_remove' pk=post.pk %}" class="post-form">{% csrf_token %}
141141
<button type="submit" class="post btn btn-danger" name="delete">
142-
<span class="glyphicon glyphicon-remove"></span>
142+
Delete
143143
</button>
144144
</form>
145145
```
@@ -149,7 +149,7 @@ just under a line with the edit button.
149149
Now we need a URL (`blog/urls.py`):
150150

151151
```python
152-
path('post/<pk>/remove/', views.post_remove, name='post_remove'),
152+
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
153153
```
154154

155155
Now, time for a view! Open `blog/views.py` and add this code:

es/homework/README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Es tiempo de hacer algo similiar, pero con borradores.
2121
Vamos a añadir un enlace en `blog/templates/blog/base.html` en el encabezado. No queremos mostrar nuestro borradores a todo el mundo, entonces vamos a colocarlo dentro de la verificación {% raw %}`{% if user.is_authenticated %}`{% endraw %}, justo después del botón de agregar posts.
2222

2323
```django
24-
<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
24+
<a href="{% url 'post_draft_list' %}" class="top-menu">Drafts</span></a>
2525
```
2626

2727
Siguiente: ¡urls! en `blog/urls.py` vamos a agregar:
@@ -84,7 +84,9 @@ por estas:
8484
{{ post.published_date }}
8585
</div>
8686
{% else %}
87-
<a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">Publish</a>
87+
<form method="POST" action="{% url 'post_publish' pk=post.pk %}" class="post-form">{% csrf_token %}
88+
<button type="submit" class="post btn btn-secondary" name="publish">Publish</button>
89+
</form>
8890
{% endif %}
8991
```
9092

@@ -93,15 +95,16 @@ Como puedes ver, hemos agregado la línea {% raw %}`{% else %}`{% endraw %}. Est
9395
Tiempo de crear una URL (en `blog/urls.py`):
9496

9597
```python
96-
path('post/<pk>/publish/', views.post_publish, name='post_publish'),
98+
path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
9799
```
98100

99101
Y finalmente una *vista* (como siempre, en `blog/views.py`):
100102

101103
```python
102104
def post_publish(request, pk):
103105
post = get_object_or_404(Post, pk=pk)
104-
post.publish()
106+
if request.method=='POST':
107+
post.publish()
105108
return redirect('post_detail', pk=pk)
106109
```
107110

@@ -126,23 +129,28 @@ Y de nuevo al publicar el post, ¡somos redirigidos inmediatamente a la página
126129
Vamos a abrir `blog/templates/blog/post_detail.html` de nuevo y vamos a añadir esta línea
127130

128131
```django
129-
<a class="btn btn-default" href="{% url 'post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
132+
<form method="POST" action="{% url 'post_remove' pk=post.pk %}" class="post-form">{% csrf_token %}
133+
<button type="submit" class="post btn btn-danger" name="delete">
134+
Delete
135+
</button>
136+
</form>
130137
```
131138

132139
Justo debajo de la línea co el botón editar.
133140

134141
Ahora necesitamos una URL (`blog/urls.py`):
135142

136143
```python
137-
path('post/<pk>/remove/', views.post_remove, name='post_remove'),
144+
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
138145
```
139146

140147
Ahora, ¡Tiempo para la vista! Abre `blog/views.py` y agrega este código:
141148

142149
```python
143150
def post_remove(request, pk):
144151
post = get_object_or_404(Post, pk=pk)
145-
post.delete()
152+
if request.method=='POST':
153+
post.delete()
146154
return redirect('post_list')
147155
```
148156

fa/homework/README.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ post.published_date = timezone.now()
2121
حالا می‌خواهیم در هدر فایل `blog/templates/blog/base.html` یک لینک اضافه کنیم. از آنجا که نمی‌خواهیم لیست پست‌های پیش‌نویس را به همه نشان بدهیم، پس این لینک رادر عبارت کنترلی {% raw %}`{% if user.is_authenticated %}`{% endraw %} و دقیقاً بعد از دکمه مربوط به اضافه کردن پست جدید، اضافه می‌کنیم.
2222

2323
```django
24-
<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
24+
<a href="{% url 'post_draft_list' %}" class="top-menu">Drafts</span></a>
2525
```
2626

2727
حالا وقت اصلاح urlها در فایل `blog/urls.py` است:
@@ -84,7 +84,9 @@ def post_draft_list(request):
8484
{{ post.published_date }}
8585
</div>
8686
{% else %}
87-
<a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">Publish</a>
87+
<form method="POST" action="{% url 'post_publish' pk=post.pk %}" class="post-form">{% csrf_token %}
88+
<button type="submit" class="post btn btn-secondary" name="publish">Publish</button>
89+
</form>
8890
{% endif %}
8991
```
9092

@@ -93,15 +95,16 @@ def post_draft_list(request):
9395
زمان ساختن یک URL (در فایل `blog/urls.py`) است:
9496

9597
```python
96-
path('post/<pk>/publish/', views.post_publish, name='post_publish'),
98+
path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
9799
```
98100

99101
و در نهایت یک *ویو* (مانند همیشه در فایل `blog/views.py`) می‌سازیم:
100102

101103
```python
102104
def post_publish(request, pk):
103105
post = get_object_or_404(Post, pk=pk)
104-
post.publish()
106+
if request.method=='POST':
107+
post.publish()
105108
return redirect('post_detail', pk=pk)
106109
```
107110

@@ -115,7 +118,7 @@ def publish(self):
115118

116119
حالا بالاخره می‌توانیم از آن استفاده کنیم!
117120

118-
و یک بار دیگر بعد از انتشار پست ما بلافاصله به صفحه جزییات پست یا `post_detail` هدایت خواهیم شد!
121+
و یک بار دیگر بعد از انتشار پست ما بلافاصله به صفحه جزییات پست یا `post_detail` هدایت خواهیم شد!
119122

120123
![Publish button](images/publish2.png)
121124

@@ -126,23 +129,28 @@ def publish(self):
126129
بیایید یک بار دیگر فایل `blog/templates/blog/post_detail.html` را باز کنید و این خط را به آن اضافه کنید:
127130

128131
```django
129-
<a class="btn btn-default" href="{% url 'post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
132+
<form method="POST" action="{% url 'post_remove' pk=post.pk %}" class="post-form">{% csrf_token %}
133+
<button type="submit" class="post btn btn-danger" name="delete">
134+
Delete
135+
</button>
136+
</form>
130137
```
131138

132139
دقیقاً زیر خطی که دکمه اصلاح یا Edit button قرار دارد.
133140

134141
حالا یک URL لازم داریم (`blog/urls.py`):
135142

136143
```python
137-
path('post/<pk>/remove/', views.post_remove, name='post_remove'),
144+
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
138145
```
139146

140147
و الان زمان اضافه کردن یک ویو است! فایل `blog/views.py` را باز کنید و این کد را به آن اضافه کنید:
141148

142149
```python
143150
def post_remove(request, pk):
144151
post = get_object_or_404(Post, pk=pk)
145-
post.delete()
152+
if request.method=='POST':
153+
post.delete()
146154
return redirect('post_list')
147155
```
148156

ja/homework/README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Djangoのクエリセットを勉強した章を覚えていますか? `post_lis
2121
`blog/templates/blog/base.html` のヘッダーにリンクを追加しましょう。草稿の一覧は誰でも見られるようにはしません。なので、 {% raw %}`{% if user.is_authenticated %}`{% endraw %} という条件の確認に続く箇所で、新しい投稿を追加するボタンのすぐ後にリンクを書いてください。
2222

2323
```django
24-
<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
24+
<a href="{% url 'post_draft_list' %}" class="top-menu">Drafts</span></a>
2525
```
2626

2727
次は `blog/urls.py` に、urlを追加しましょう!
@@ -84,7 +84,9 @@ def post_draft_list(request):
8484
{{ post.published_date }}
8585
</div>
8686
{% else %}
87-
<a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">Publish</a>
87+
<form method="POST" action="{% url 'post_publish' pk=post.pk %}" class="post-form">{% csrf_token %}
88+
<button type="submit" class="post btn btn-secondary" name="publish">Publish</button>
89+
</form>
8890
{% endif %}
8991
```
9092

@@ -93,15 +95,16 @@ def post_draft_list(request):
9395
それでは新しいURLを追加しましょう。( `blog/urls.py` に)
9496

9597
```python
96-
path('post/<pk>/publish/', views.post_publish, name='post_publish'),
98+
path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
9799
```
98100

99101
最後に *ビュー* を追加します。(いつものように `blog/views.py` に)
100102

101103
```python
102104
def post_publish(request, pk):
103105
post = get_object_or_404(Post, pk=pk)
104-
post.publish()
106+
if request.method=='POST':
107+
post.publish()
105108
return redirect('post_detail', pk=pk)
106109
```
107110

@@ -127,21 +130,26 @@ def publish(self):
127130
下の行を編集ボタンの行の直後に追加します:
128131

129132
```django
130-
<a class="btn btn-default" href="{% url 'post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
133+
<form method="POST" action="{% url 'post_remove' pk=post.pk %}" class="post-form">{% csrf_token %}
134+
<button type="submit" class="post btn btn-danger" name="delete">
135+
Delete
136+
</button>
137+
</form>
131138
```
132139

133140
URLも必要ですね。( `blog/urls.py` に)
134141

135142
```python
136-
path('post/<pk>/remove/', views.post_remove, name='post_remove'),
143+
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
137144
```
138145

139146
次はビューも作りましょう。 `blog/views.py` を開いて下のコードを追加してください。
140147

141148
```python
142149
def post_remove(request, pk):
143150
post = get_object_or_404(Post, pk=pk)
144-
post.delete()
151+
if request.method=='POST':
152+
post.delete()
145153
return redirect('post_list')
146154
```
147155

ko/homework/README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ post.published_date = timezone.now()
2222
새 글 추가하기 버튼 근처에 `blog/templates/blog/base.html` 링크를 추가하세요({% raw %}`<h1><a href="/">Django Girls Blog</a></h1>`{% endraw %}위에 바로 추가하면 됩니다!). 발행 전 미리 보기가 모두에게 보이는 걸 원치 않을 거예요. 새로운 글 추가하기 바로 아래에 {% raw %}`{% if user.is_authenticated %}`{% endraw %}을 추가해 주세요.
2323

2424
```django
25-
<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
25+
<a href="{% url 'post_draft_list' %}" class="top-menu">Drafts</span></a>
2626
```
2727

2828
다음: url입니다! `blog/urls.py`을 열고 아래 내용을 추가할 거에요.
2929

3030
```python
31-
url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
31+
path('drafts/', views.post_draft_list, name='post_draft_list'),
3232
```
3333

3434
`blog/views.py`에 view를 생성할 차례입니다.
@@ -59,7 +59,7 @@ def post_draft_list(request):
5959

6060
`post_list.html` 템플릿과 코드가 많이 비슷해보이죠?
6161

62-
브라우저로 `http://127.0.0.1:8000/draft/` 페이지를 열어보면, 미 게시된 글목록을 확인할 수 있어요.
62+
브라우저로 `http://127.0.0.1:8000/drafts/` 페이지를 열어보면, 미 게시된 글목록을 확인할 수 있어요.
6363

6464
야호! 첫 번째 일이 마쳤어요!
6565

@@ -85,7 +85,9 @@ def post_draft_list(request):
8585
{{ post.published_date }}
8686
</div>
8787
{% else %}
88-
<a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">Publish</a>
88+
<form method="POST" action="{% url 'post_publish' pk=post.pk %}" class="post-form">{% csrf_token %}
89+
<button type="submit" class="post btn btn-secondary" name="publish">Publish</button>
90+
</form>
8991
{% endif %}
9092
```
9193

@@ -94,15 +96,16 @@ def post_draft_list(request):
9496
`blog/urls.py`에 URL 패턴을 추가해봅시다.
9597

9698
```python
97-
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
99+
path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
98100
```
99101

100102
마지막으로 `post_publish` **`blog/views.py` 에 추가해봅시다.
101103

102104
```python
103105
def post_publish(request, pk):
104106
post = get_object_or_404(Post, pk=pk)
105-
post.publish()
107+
if request.method=='POST':
108+
post.publish()
106109
return redirect('post_detail', pk=pk)
107110
```
108111

@@ -127,23 +130,28 @@ def publish(self):
127130
`blog/templates/blog/post_detail.html` 파일에 아래 코드를 추가해주세요.
128131

129132
```django
130-
<a class="btn btn-default" href="{% url 'post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
133+
<form method="POST" action="{% url 'post_remove' pk=post.pk %}" class="post-form">{% csrf_token %}
134+
<button type="submit" class="post btn btn-danger" name="delete">
135+
Delete
136+
</button>
137+
</form>
131138
```
132139

133140
수정 버튼 바로 아래 줄에 추가해주세요.
134141

135142
(`blog/urls.py`)에 URL 패턴을 추가해봅시다:
136143

137144
```python
138-
url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),
145+
path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
139146
```
140147

141148
이제 post_remove 뷰를 구현해봅시다. `blog/views.py` 에 아래 코드를 추가해주세요.
142149

143150
```python
144151
def post_remove(request, pk):
145152
post = get_object_or_404(Post, pk=pk)
146-
post.delete()
153+
if request.method=='POST':
154+
post.delete()
147155
return redirect('post_list')
148156
```
149157

0 commit comments

Comments
 (0)