回答編集履歴

1

サンプル追加

2020/08/28 15:41

投稿

退会済みユーザー
test CHANGED
@@ -45,3 +45,357 @@
45
45
 
46
46
 
47
47
  のような形になります。xxxx.htmlテンプレートを使用するviewで、オブジェクトのプロパティにdicを設定してあげることで、リンクに反映され、ルーティングで読み取られ、viewに渡ります。
48
+
49
+
50
+
51
+ 一応前にコメントしたサンプルに引数langをダミーで加えたバージョンを付記しておきます。
52
+
53
+ ```bash
54
+
55
+ PYTHONCOMMAND="python3"
56
+
57
+ if ! command -v python3; then
58
+
59
+ PYTHONCOMMAND="python"
60
+
61
+ fi
62
+
63
+ $PYTHONCOMMAND -m venv env
64
+
65
+ . env/bin/activate
66
+
67
+ pip install --upgrade pip
68
+
69
+ pip install django==3.1
70
+
71
+ django-admin startproject mysite
72
+
73
+ cd mysite
74
+
75
+ django-admin startapp myapp
76
+
77
+ patch -p1 << __END_OF_TERMINAL_CODE__
78
+
79
+ diff --git a/myapp.WordDefinition.json b/myapp.WordDefinition.json
80
+
81
+ new file mode 100644
82
+
83
+ index 0000000..34d0aaa
84
+
85
+ --- /dev/null
86
+
87
+ +++ b/myapp.WordDefinition.json
88
+
89
+ @@ -0,0 +1,2 @@
90
+
91
+ +[{"model": "myapp.worddefinition", "pk": 1, "fields": {"word": "単語1", "definition": "単語1の定義"}}, {"model": "myapp.worddefinition", "pk": 2, "fields": {"word": "単語2", "definition": "単語2の定義"}}, {"model": "myapp.worddefinition", "pk": 3, "fields": {"word": "単語3", "definition": "単語3の定義"}}]
92
+
93
+ +
94
+
95
+ diff --git a/myapp/admin.py b/myapp/admin.py
96
+
97
+ index 8c38f3f..85d969d 100644
98
+
99
+ --- a/myapp/admin.py
100
+
101
+ +++ b/myapp/admin.py
102
+
103
+ @@ -1,3 +1,7 @@
104
+
105
+ +"""
106
+
107
+ +admin.py
108
+
109
+ +"""
110
+
111
+ from django.contrib import admin
112
+
113
+ +from .models import WordDefinition
114
+
115
+
116
+
117
+ -# Register your models here.
118
+
119
+ +admin.site.register(WordDefinition)
120
+
121
+ diff --git a/myapp/models.py b/myapp/models.py
122
+
123
+ index 71a8362..cd2b051 100644
124
+
125
+ --- a/myapp/models.py
126
+
127
+ +++ b/myapp/models.py
128
+
129
+ @@ -1,3 +1,11 @@
130
+
131
+ +"""
132
+
133
+ +models.py
134
+
135
+ +"""
136
+
137
+ from django.db import models
138
+
139
+
140
+
141
+ -# Create your models here.
142
+
143
+ +class WordDefinition(models.Model):
144
+
145
+ + """
146
+
147
+ + Word and its Definition
148
+
149
+ + """
150
+
151
+ + word = models.CharField('単語', max_length=200, unique=True, null=False)
152
+
153
+ + definition = models.CharField('定義', max_length=1024, null=False)
154
+
155
+ diff --git a/myapp/templates/myapp/index.html b/myapp/templates/myapp/index.html
156
+
157
+ new file mode 100644
158
+
159
+ index 0000000..a76e60d
160
+
161
+ --- /dev/null
162
+
163
+ +++ b/myapp/templates/myapp/index.html
164
+
165
+ @@ -0,0 +1,24 @@
166
+
167
+ +<!DOCTYPE html>
168
+
169
+ +<html lang="ja">
170
+
171
+ +
172
+
173
+ +<head>
174
+
175
+ + <meta charset="utf-8">
176
+
177
+ +</head>
178
+
179
+ +
180
+
181
+ +<body>
182
+
183
+ + {% if all_words %}
184
+
185
+ + <table class="table table-bordered">
186
+
187
+ + {% for w in all_words %}
188
+
189
+ + <tr>
190
+
191
+ + <td><a>{{ w.word }}</a></td>
192
+
193
+ + <td><a>{{ w.definition }}</a></td>
194
+
195
+ + <td style="text-align: center;">
196
+
197
+ + <a href="{% url 'delete' lang w.id %}">Delete</a>
198
+
199
+ + </td>
200
+
201
+ + </tr>
202
+
203
+ + {% endfor %}
204
+
205
+ + </table>
206
+
207
+ + {% endif %}
208
+
209
+ +</body>
210
+
211
+ +
212
+
213
+ +</html>
214
+
215
+
216
+
217
+ diff --git a/myapp/urls.py b/myapp/urls.py
218
+
219
+ new file mode 100644
220
+
221
+ index 0000000..5e7a908
222
+
223
+ --- /dev/null
224
+
225
+ +++ b/myapp/urls.py
226
+
227
+ @@ -0,0 +1,10 @@
228
+
229
+ +"""
230
+
231
+ +urls.py
232
+
233
+ +"""
234
+
235
+ +from django.urls import path
236
+
237
+ +from . import views
238
+
239
+ +
240
+
241
+ +urlpatterns = [
242
+
243
+ + path('', views.index, name='index'),
244
+
245
+ + path('delete/<lang>/<word_id>', views.delete, name = 'delete')
246
+
247
+ +]
248
+
249
+ diff --git a/myapp/views.py b/myapp/views.py
250
+
251
+ index 91ea44a..6e68633 100644
252
+
253
+ --- a/myapp/views.py
254
+
255
+ +++ b/myapp/views.py
256
+
257
+ @@ -1,3 +1,25 @@
258
+
259
+ +"""
260
+
261
+ +View
262
+
263
+ +"""
264
+
265
+ from django.shortcuts import render
266
+
267
+ +from django.http import HttpResponse
268
+
269
+ +from django.urls import reverse
270
+
271
+ +from .models import WordDefinition
272
+
273
+
274
+
275
+ -# Create your views here.
276
+
277
+ +def index(request):
278
+
279
+ + """
280
+
281
+ + list words and links for deletion.
282
+
283
+ + """
284
+
285
+ + words = WordDefinition.objects.all()
286
+
287
+ + return render(request, 'myapp/index.html', {"all_words": words, "lang": "hoge"})
288
+
289
+ +
290
+
291
+ +def delete(request, lang, word_id):
292
+
293
+ + """
294
+
295
+ + delete the specified word.
296
+
297
+ + """
298
+
299
+ + word = WordDefinition.objects.get(id=word_id)
300
+
301
+ + word.delete()
302
+
303
+ +
304
+
305
+ + return HttpResponse('<div>Word Has Been Deleted!</div>'\
306
+
307
+ + '<div><a href="{url}">back to index</a>{lang}</div>'
308
+
309
+ + .format(url=reverse('index'), lang=lang))
310
+
311
+ diff --git a/mysite/settings.py b/mysite/settings.py
312
+
313
+ index 5680ac4..20a2808 100644
314
+
315
+ --- a/mysite/settings.py
316
+
317
+ +++ b/mysite/settings.py
318
+
319
+ @@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
320
+
321
+ # Application definition
322
+
323
+
324
+
325
+ INSTALLED_APPS = [
326
+
327
+ + 'myapp.apps.MyappConfig',
328
+
329
+ 'django.contrib.admin',
330
+
331
+ 'django.contrib.auth',
332
+
333
+ 'django.contrib.contenttypes',
334
+
335
+ @@ -103,9 +104,9 @@ AUTH_PASSWORD_VALIDATORS = [
336
+
337
+ # Internationalization
338
+
339
+ # https://docs.djangoproject.com/en/3.1/topics/i18n/
340
+
341
+
342
+
343
+ -LANGUAGE_CODE = 'en-us'
344
+
345
+ +LANGUAGE_CODE = 'ja-jp'
346
+
347
+
348
+
349
+ -TIME_ZONE = 'UTC'
350
+
351
+ +TIME_ZONE = 'Asia/Tokyo'
352
+
353
+
354
+
355
+ USE_I18N = True
356
+
357
+
358
+
359
+ diff --git a/mysite/urls.py b/mysite/urls.py
360
+
361
+ index e7f9f35..b41a8b1 100644
362
+
363
+ --- a/mysite/urls.py
364
+
365
+ +++ b/mysite/urls.py
366
+
367
+ @@ -14,8 +14,9 @@ Including another URLconf
368
+
369
+ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
370
+
371
+ """
372
+
373
+ from django.contrib import admin
374
+
375
+ -from django.urls import path
376
+
377
+ +from django.urls import path, include
378
+
379
+
380
+
381
+ urlpatterns = [
382
+
383
+ + path('myapp/', include('myapp.urls')),
384
+
385
+ path('admin/', admin.site.urls),
386
+
387
+ ]
388
+
389
+ __END_OF_TERMINAL_CODE__
390
+
391
+ python manage.py makemigrations
392
+
393
+ python manage.py migrate
394
+
395
+ python manage.py loaddata myapp.WordDefinition.json
396
+
397
+ python manage.py runserver
398
+
399
+
400
+
401
+ ```