テンプレートでのurlタグについて複数の引数を取る構文は、
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url
のとおりです。今回のケースでは、
urls.py
python
1urlpatterns = [
2 path('delete/<dic>/<id>', views.delete, name='delete'),
3]
views.py
python
1def delete(request, dic, id):
xxxx.html
python
1<a href="{% url 'delete' dic w.id %}">Delete</a>
のような形になります。xxxx.htmlテンプレートを使用するviewで、オブジェクトのプロパティにdicを設定してあげることで、リンクに反映され、ルーティングで読み取られ、viewに渡ります。
一応前にコメントしたサンプルに引数langをダミーで加えたバージョンを付記しておきます。
bash
1PYTHONCOMMAND="python3"
2if ! command -v python3; then
3 PYTHONCOMMAND="python"
4fi
5$PYTHONCOMMAND -m venv env
6. env/bin/activate
7pip install --upgrade pip
8pip install django==3.1
9django-admin startproject mysite
10cd mysite
11django-admin startapp myapp
12patch -p1 << __END_OF_TERMINAL_CODE__
13diff --git a/myapp.WordDefinition.json b/myapp.WordDefinition.json
14new file mode 100644
15index 0000000..34d0aaa
16--- /dev/null
17+++ b/myapp.WordDefinition.json
18@@ -0,0 +1,2 @@
19+[{"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の定義"}}]
20+
21diff --git a/myapp/admin.py b/myapp/admin.py
22index 8c38f3f..85d969d 100644
23--- a/myapp/admin.py
24+++ b/myapp/admin.py
25@@ -1,3 +1,7 @@
26+"""
27+admin.py
28+"""
29 from django.contrib import admin
30+from .models import WordDefinition
31
32-# Register your models here.
33+admin.site.register(WordDefinition)
34diff --git a/myapp/models.py b/myapp/models.py
35index 71a8362..cd2b051 100644
36--- a/myapp/models.py
37+++ b/myapp/models.py
38@@ -1,3 +1,11 @@
39+"""
40+models.py
41+"""
42 from django.db import models
43
44-# Create your models here.
45+class WordDefinition(models.Model):
46+ """
47+ Word and its Definition
48+ """
49+ word = models.CharField('単語', max_length=200, unique=True, null=False)
50+ definition = models.CharField('定義', max_length=1024, null=False)
51diff --git a/myapp/templates/myapp/index.html b/myapp/templates/myapp/index.html
52new file mode 100644
53index 0000000..a76e60d
54--- /dev/null
55+++ b/myapp/templates/myapp/index.html
56@@ -0,0 +1,24 @@
57+<!DOCTYPE html>
58+<html lang="ja">
59+
60+<head>
61+ <meta charset="utf-8">
62+</head>
63+
64+<body>
65+ {% if all_words %}
66+ <table class="table table-bordered">
67+ {% for w in all_words %}
68+ <tr>
69+ <td><a>{{ w.word }}</a></td>
70+ <td><a>{{ w.definition }}</a></td>
71+ <td style="text-align: center;">
72+ <a href="{% url 'delete' lang w.id %}">Delete</a>
73+ </td>
74+ </tr>
75+ {% endfor %}
76+ </table>
77+ {% endif %}
78+</body>
79+
80+</html>
81\ No newline at end of file
82diff --git a/myapp/urls.py b/myapp/urls.py
83new file mode 100644
84index 0000000..5e7a908
85--- /dev/null
86+++ b/myapp/urls.py
87@@ -0,0 +1,10 @@
88+"""
89+urls.py
90+"""
91+from django.urls import path
92+from . import views
93+
94+urlpatterns = [
95+ path('', views.index, name='index'),
96+ path('delete/<lang>/<word_id>', views.delete, name = 'delete')
97+]
98diff --git a/myapp/views.py b/myapp/views.py
99index 91ea44a..6e68633 100644
100--- a/myapp/views.py
101+++ b/myapp/views.py
102@@ -1,3 +1,25 @@
103+"""
104+View
105+"""
106 from django.shortcuts import render
107+from django.http import HttpResponse
108+from django.urls import reverse
109+from .models import WordDefinition
110
111-# Create your views here.
112+def index(request):
113+ """
114+ list words and links for deletion.
115+ """
116+ words = WordDefinition.objects.all()
117+ return render(request, 'myapp/index.html', {"all_words": words, "lang": "hoge"})
118+
119+def delete(request, lang, word_id):
120+ """
121+ delete the specified word.
122+ """
123+ word = WordDefinition.objects.get(id=word_id)
124+ word.delete()
125+
126+ return HttpResponse('<div>Word Has Been Deleted!</div>'\
127+ '<div><a href="{url}">back to index</a>{lang}</div>'
128+ .format(url=reverse('index'), lang=lang))
129diff --git a/mysite/settings.py b/mysite/settings.py
130index 5680ac4..20a2808 100644
131--- a/mysite/settings.py
132+++ b/mysite/settings.py
133@@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
134 # Application definition
135
136 INSTALLED_APPS = [
137+ 'myapp.apps.MyappConfig',
138 'django.contrib.admin',
139 'django.contrib.auth',
140 'django.contrib.contenttypes',
141@@ -103,9 +104,9 @@ AUTH_PASSWORD_VALIDATORS = [
142 # Internationalization
143 # https://docs.djangoproject.com/en/3.1/topics/i18n/
144
145-LANGUAGE_CODE = 'en-us'
146+LANGUAGE_CODE = 'ja-jp'
147
148-TIME_ZONE = 'UTC'
149+TIME_ZONE = 'Asia/Tokyo'
150
151 USE_I18N = True
152
153diff --git a/mysite/urls.py b/mysite/urls.py
154index e7f9f35..b41a8b1 100644
155--- a/mysite/urls.py
156+++ b/mysite/urls.py
157@@ -14,8 +14,9 @@ Including another URLconf
158 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
159 """
160 from django.contrib import admin
161-from django.urls import path
162+from django.urls import path, include
163
164 urlpatterns = [
165+ path('myapp/', include('myapp.urls')),
166 path('admin/', admin.site.urls),
167 ]
168__END_OF_TERMINAL_CODE__
169python manage.py makemigrations
170python manage.py migrate
171python manage.py loaddata myapp.WordDefinition.json
172python manage.py runserver
173