前提・実現したいこと
Djangoでurl ~~~/app_name/1 のようにidを指定してAreaCodeのarea_name_jaを表示したい
発生している問題・エラーメッセージ
TypeError at /app_name/1/ Field 'id' expected a number but got <django.db.models.query_utils.DeferredAttribute object at 0x102549a00>. Request Method: GET Request URL: http://127.0.0.1:8000/app_name/1/ Django Version: 3.0.5 Exception Type: TypeError Exception Value: Field 'id' expected a number but got <django.db.models.query_utils.DeferredAttribute object at 0x102549a00>. Exception Location: /Users/fuminori/.pyenv/versions/3.9.0a5/lib/python3.9/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 1774 Python Executable: /Users/fuminori/.pyenv/versions/3.9.0a5/bin/python Python Version: 3.9.0 Python Path: ['/Users/fuminori/ember', '/Users/fuminori/.pyenv/versions/3.9.0a5/lib/python39.zip', '/Users/fuminori/.pyenv/versions/3.9.0a5/lib/python3.9', '/Users/fuminori/.pyenv/versions/3.9.0a5/lib/python3.9/lib-dynload', '/Users/fuminori/.pyenv/versions/3.9.0a5/lib/python3.9/site-packages'] Server time: 水, 6 5月 2020 22:10:57 +0900 Server time: 水, 6 5月 2020 22:10:57 +0900
該当のソースコード
app_name/models.py
Python
1from django.db import models 2 3class AreaCode(models.Model): 4 id = models.AutoField(primary_key=True) 5 area_name_ja = models.CharField(max_length=20) 6 area_name_en = models.CharField(max_length=20) 7 show_area_id = models.CharField(max_length=100)
app_name/urls.py
Python
1from django.urls import path 2from . import views 3 4urlpatterns = [ 5 path('', views.index, name='index'), 6 # ex: /polls/5/ 7 path('<int:OrgType_id>/', views.detail, name='detail'), 8 # ex: /polls/5/results/ 9 path('<int:OrgType_id>/results/', views.results, name='results'), 10 # ex: /polls/5/vote/ 11 path('<int:OrgType_id>/vote/', views.vote, name='vote'), 12]
app_name/view.py
Python
1from django.http import Http404 2from django.http import HttpResponse 3from django.template import loader 4from django.shortcuts import get_object_or_404, render 5from .models import AreaCode 6 7def index(request): 8 return render(request, 'app_name/index.html') 9 10def detail(request, OrgType_id): 11 12 area = get_object_or_404(AreaCode, pk=AreaCode.id) 13 return render(request, 'app_name/detail.html', {'area': area}) 14 15def results(request, OrgType_id): 16 response = "You're looking at the results of question %s." 17 return HttpResponse(response % AreaCode.id) 18 19def vote(request, question_id): 20 return HttpResponse("You're voting on question %s." % AreaCode.id)
DBに値は入っています。
回答1件
あなたの回答
tips
プレビュー