teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

teratailの使い方を間違ってました。自己解決の投稿。

2020/01/23 15:25

投稿

JironBach
JironBach

スコア11

title CHANGED
File without changes
body CHANGED
@@ -131,54 +131,4 @@
131
131
  path('', include('testapp.views.urls')),
132
132
  ]
133
133
  ```
134
- がありました。
134
+ がありました。
135
-
136
- # 自己解決しました。
137
- ## 下記修正は質問とは異なる修正も含まれますが、とにかくpostしたデータをデータベースに更新できたコードも含めて記載しておきます。
138
- ## testapp/user_views.py
139
- ###下記の修正はupdateが呼ばれなかったことに関する修正です。
140
- ``` python
141
- def update(self, request, *args, **kwargs):
142
-
143
- def update(request):
144
- ```
145
-
146
- 下記はuser_views.pyのコードです。
147
- ``` python
148
- def update(request):
149
- print('updateに入った')
150
-
151
- userid = request.POST.get('userid', None)
152
- # filterを修正する時にググったコードをコピペしてしまったのがおかしかった。
153
- # filter_containsをid__containsに修正しました。
154
- # その後のイコールに余計なコードが入っていたので簡素化しました。
155
- user = AppUser.objects.filter(id__contains=userid).first()
156
- user.name = request.POST.get('username', None)
157
- user.save()
158
- # redirectのコードを簡素化
159
- return HttpResponseRedirect('/')
160
- ```
161
-
162
- 下記はhtmlのテーブルのデータをpostした修正です。
163
- ## testapp/templates.html
164
- ``` html
165
- <tbody>
166
- {% for user in users %}
167
- <form method="post" action="update/">
168
- {% csrf_token %}
169
- <tr>
170
- <td>
171
- {{ user.id }}
172
- <input type="hidden" name="userid" id="userid" value="{{ user.id }}"/>
173
- </td>
174
- <td>
175
- <input type="text" name="username" id="username" value="{{ user.name }}"/>
176
- </td>
177
- <td>{{ user.created_at }}</td>
178
- <td>{{ user.updated_at }}</td>
179
- <td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td>
180
- </tr>
181
- </form>
182
- {% endfor %}
183
- </tbody>
184
- ```

2

viewsのpostが呼ばれなかったことに関する修正。redirectを簡素化。

2020/01/23 15:25

投稿

JironBach
JironBach

スコア11

title CHANGED
File without changes
body CHANGED
@@ -131,4 +131,54 @@
131
131
  path('', include('testapp.views.urls')),
132
132
  ]
133
133
  ```
134
- がありました。
134
+ がありました。
135
+
136
+ # 自己解決しました。
137
+ ## 下記修正は質問とは異なる修正も含まれますが、とにかくpostしたデータをデータベースに更新できたコードも含めて記載しておきます。
138
+ ## testapp/user_views.py
139
+ ###下記の修正はupdateが呼ばれなかったことに関する修正です。
140
+ ``` python
141
+ def update(self, request, *args, **kwargs):
142
+
143
+ def update(request):
144
+ ```
145
+
146
+ 下記はuser_views.pyのコードです。
147
+ ``` python
148
+ def update(request):
149
+ print('updateに入った')
150
+
151
+ userid = request.POST.get('userid', None)
152
+ # filterを修正する時にググったコードをコピペしてしまったのがおかしかった。
153
+ # filter_containsをid__containsに修正しました。
154
+ # その後のイコールに余計なコードが入っていたので簡素化しました。
155
+ user = AppUser.objects.filter(id__contains=userid).first()
156
+ user.name = request.POST.get('username', None)
157
+ user.save()
158
+ # redirectのコードを簡素化
159
+ return HttpResponseRedirect('/')
160
+ ```
161
+
162
+ 下記はhtmlのテーブルのデータをpostした修正です。
163
+ ## testapp/templates.html
164
+ ``` html
165
+ <tbody>
166
+ {% for user in users %}
167
+ <form method="post" action="update/">
168
+ {% csrf_token %}
169
+ <tr>
170
+ <td>
171
+ {{ user.id }}
172
+ <input type="hidden" name="userid" id="userid" value="{{ user.id }}"/>
173
+ </td>
174
+ <td>
175
+ <input type="text" name="username" id="username" value="{{ user.name }}"/>
176
+ </td>
177
+ <td>{{ user.created_at }}</td>
178
+ <td>{{ user.updated_at }}</td>
179
+ <td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td>
180
+ </tr>
181
+ </form>
182
+ {% endfor %}
183
+ </tbody>
184
+ ```

1

models, views, templatesのディレクトリを分けてるので、質問がややこしくなってしまったようです。

2020/01/23 15:24

投稿

JironBach
JironBach

スコア11

title CHANGED
File without changes
body CHANGED
@@ -116,4 +116,19 @@
116
116
  ```
117
117
 
118
118
  以上でした。
119
- すみませんが、どうかよろしくお願いします。
119
+ すみませんが、どうかよろしくお願いします。
120
+
121
+ ### 質問(追記)
122
+ updateというメソッドを呼び出してmodelを更新したいのですが方法がわかりません。
123
+
124
+ 情報が少し足りなかったかもしれませんので追記です。
125
+ testapp/urls.py
126
+ ```
127
+ from django.conf.urls import url
128
+ from django.urls import include, path
129
+
130
+ urlpatterns = [
131
+ path('', include('testapp.views.urls')),
132
+ ]
133
+ ```
134
+ がありました。