質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1586閲覧

Djangoのpostでエラーがでます

JironBach

総合スコア11

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/01/22 13:15

編集2020/01/23 15:25

Djangoでpostしたデータでmodelをupdateしたいのですがpostでエラーがでます。

質問が2つあるのですが、urlsが効いてないらしいのと、updateが呼ばれてないらしいことです。

testapp/views/urls.py

python

1from django.conf.urls import url 2from django.urls import include, path 3from . import views 4from testapp.views.user_views import UserViews 5from testapp.views.get_views import GetViews 6 7urlpatterns = [ 8 path('', UserViews.index, name='index'), 9 path('app0/', GetViews.as_view(), ), 10 path('app1/', UserViews.index, name='index'), 11 12 path('update/', UserViews.update, name='update'), 13]

testapp/templates/users.html

html

1{% extends "base.html" %} 2 3{% block content %} 4 <form method="post" action="update/"> 5 {% csrf_token %} 6 <table border="1"> 7 <thead> 8 <tr> 9 <th>id</th> 10 <th>name</th> 11 <th>created_at</th> 12 <th>updated_at</th> 13 <th>更新</th> 14 </tr> 15 </thead> 16 <tbody> 17 {% for user in users %} 18 <tr> 19 <td> 20 {{ user.id }} 21 <input type="hidden" name="id" id="id" value="{{ user.id }}"/> 22 </td> 23 <td> 24 <input type="text" name="username" id="username" value="{{ user.name }}"/> 25 </td> 26 <td>{{ user.created_at }}</td> 27 <td>{{ user.updated_at }}</td> 28 <td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td> 29 </tr> 30 {% endfor %} 31 </tbody> 32 </table> 33 名前 <input type="textbox" value="{{ user.name }}" name="username" /> 34 <input type="submit" name="commit" value="登録" data-disable-with="登録" /> 35 </form> 36{% endblock %}

testapp/views/user_views.html

html

1from django.shortcuts import render 2from django.http import HttpResponse, HttpResponseRedirect 3from testapp.models.app_user import AppUser 4from . import views 5from django.views.generic import TemplateView 6from django.urls import reverse 7 8class UserViews(TemplateView): 9 def index(request): 10 print('indexに入った') 11 users = { 12 'users': AppUser.objects.all(), 13 } 14 return render(request, 'users.html', users) 15 16 def update(self, request, *args, **kwargs): 17 print('updateに入った') 18 19 user.id = AppUser.objects.filter(field__contains="id=request.POST.get('id', None)").first() 20 user.name = request.POST.get('name', None) 21 user.save() 22 redirect_url = reverse('update') #'testapp:user_views') 23 parameters = urlencode({'id': user.id, 'name': user.name}) 24 url = "{redirect_url}?{parameters}" 25 return redirect(url)

エラーログ

TypeError at /update/ update() missing 1 required positional argument: 'request' Request Method: POST Request URL: http://localhost:8000/update/ Django Version: 2.2.2 Exception Type: TypeError Exception Value: update() missing 1 required positional argument: 'request' Exception Location: /Users/js/.pyenv/versions/TensorFlow/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 113 Python Executable: /Users/js/.pyenv/versions/TensorFlow/bin/python Python Version: 3.6.5 Python Path: ['/Users/js/work/django_proj', '/Users/js/.pyenv/versions/TensorFlow/lib/python36.zip', '/Users/js/.pyenv/versions/TensorFlow/lib/python3.6', '/Users/js/.pyenv/versions/TensorFlow/lib/python3.6/lib-dynload', '/Users/js/.local/lib/python3.6/site-packages', '/Users/js/.pyenv/versions/TensorFlow/lib/python3.6/site-packages', '/Users/js/.pyenv/versions/TensorFlow/lib/python3.6/site-packages/six-1.11.0-py3.6.egg'] Server time: 水, 22 1月 2020 22:01:52 +0900

以上でした。
すみませんが、どうかよろしくお願いします。

質問(追記)

updateというメソッドを呼び出してmodelを更新したいのですが方法がわかりません。

情報が少し足りなかったかもしれませんので追記です。
testapp/urls.py

from django.conf.urls import url from django.urls import include, path urlpatterns = [ path('', include('testapp.views.urls')), ]

がありました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dameo

2020/01/23 01:20

見出しに書かれているパスは本当に合っていますか? プロジェクトとアプリケーションはそれぞれどう構成されていますか?
JironBach

2020/01/23 02:10

manage.pyが存在するディレクトリはdjango_projで その下にprojとtestappがあります。 testappの下にmodels, templates, viewsディレクトリがそれぞれあります。 こんな情報で足りてますか?
dameo

2020/01/23 02:15

合っているということであれば、私から言えることは何もありません。 明らかに間違っているので。
quickquip

2020/01/23 03:08

質問が書いてないですね
JironBach

2020/01/23 03:59 編集

teratailの使い方がわかってませんでした。 質問に修正・追記します。
guest

回答1

0

自己解決

自己解決しました。

下記修正は質問とは異なる修正も含まれますが、とにかくpostしたデータをデータベースに更新できたコードも含めて記載しておきます。

testapp/user_views.py

###下記の修正はupdateが呼ばれなかったことに関する修正です。

python

1 def update(self, request, *args, **kwargs): 23 def update(request):

下記はuser_views.pyのコードです。

python

1 def update(request): 2 print('updateに入った') 3 4 userid = request.POST.get('userid', None) 5 # filterを修正する時にググったコードをコピペしてしまったのがおかしかった。 6 # filter_containsをid__containsに修正しました。 7 # その後のイコールに余計なコードが入っていたので簡素化しました。 8 user = AppUser.objects.filter(id__contains=userid).first() 9 user.name = request.POST.get('username', None) 10 user.save() 11 # redirectのコードを簡素化 12 return HttpResponseRedirect('/')

下記はhtmlのテーブルのデータをpostした修正です。

testapp/templates.html

html

1 <tbody> 2 {% for user in users %} 3 <form method="post" action="update/"> 4 {% csrf_token %} 5 <tr> 6 <td> 7 {{ user.id }} 8 <input type="hidden" name="userid" id="userid" value="{{ user.id }}"/> 9 </td> 10 <td> 11 <input type="text" name="username" id="username" value="{{ user.name }}"/> 12 </td> 13 <td>{{ user.created_at }}</td> 14 <td>{{ user.updated_at }}</td> 15 <td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td> 16 </tr> 17 </form> 18 {% endfor %} 19 </tbody>

投稿2020/01/23 15:26

JironBach

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

quickquip

2020/01/23 22:21

TemplateViewを使わない選択をしたと思うのですが class UserViews(TemplateView): は残したままですか?
JironBach

2020/01/30 03:28 編集

返事遅れました。コメントありがとうございます。 まだTemplateViewがわからないままサブクラスにしてしまいました。 おっしゃる通り View にしても問題なく動作したので TemplateViewは必要なかったようです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問