前提・実現したいこと
TodoModelのデータをDetailViewを継承したclassで指定したdetail.htmlファイルを実行したページを
URLで指定をして正しく表示させたい。
同じようなエラーを検索したけれどいまいちわからない
発生している問題・エラーメッセージ
ImproperlyConfigured at /detail/1 TodoDetail is missing a QuerySet. Define TodoDetail.model, TodoDetail.queryset, or override TodoDetail.get_queryset(). Request Method: GET Request URL: http://127.0.0.1:8000/detail/1 Django Version: 3.0.8 Exception Type: ImproperlyConfigured Exception Value: TodoDetail is missing a QuerySet. Define TodoDetail.model, TodoDetail.queryset, or override TodoDetail.get_queryset().
該当のソースコード
1.todo/templates/detail.html {{ object.title }} {{ object.memo }} 2.todo/todoapp/admin.py from django.contrib import admin from .models import TodoModel # Register your models here. admin.site.register(TodoModel) 3.todo/todoapp/models.py from django.db import models # Create your models here. class TodoModel(models.Model): title = models.CharField(max_length=100) memo = models.TextField() def __str__(self): return self.title 4.todo/todoapp/urls.py from django.urls import path from .views import TodoList,TodoDetail urlpatterns = [ path('list/', TodoList.as_view()), path('detail/<int:pk>',TodoDetail.as_view()), ] 5.todo/todoapp/views.py from django.shortcuts import render from django.views.generic import ListView,DetailView from .models import TodoModel # Create your views here. class TodoList(ListView): template_name = 'list.html' model = TodoModel class TodoDetail(DetailView): template_name = 'detail.html' mdoel = TodoModel 6.todo/todoproject/settings.py """ Django settings for todoproject project. Generated by 'django-admin startproject' using Django 3.0.8. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '+lkez-e@r4cu_s02#_regk$0+mq=81@*299$jy7e2x6-_h@401' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'todoapp' ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'todoproject.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR, 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'todoproject.wsgi.application' # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/3.0/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' 7.todo/todoproject/urls.py from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('', include('todoapp.urls') ), ]
試したこと
エラー名を検索して同じような問題が載ってないか調べた
補足情報(FW/ツールのバージョンなど)
OS:Linux(Windows10上)
ターミナル:bash
IDE:VSCode
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。