get_object_or_404()的第一个参数必须是Model,Manager或QuerySet,而不是“ function”。
我正在尝试根据数据库中的平板电脑显示平板电脑详细信息,但它一直在显示此ValueError。谁能帮助我找出我做错了什么?
这是我的views.py
from django.shortcuts import render, get_object_or_404
from .models import PharmaceuticalCapsules, PharmaceuticalSuspension,
PharmaceuticalPowder, PharmaceuticalSyrup, \
PharmaceuticalTablet
def alltablet(request):
tablet = PharmaceuticalTablet.objects
return render(request, 'tablets.html', {'tablet': tablet})
def tabdetail(request, tab_id):
tabdetail = get_object_or_404(alltablet, pk=tab_id)
return render(request, 'tab_detail.html', {'tablet': tabdetail})
这是我的模特。py
from typing import re
from django.db import models
class PharmaceuticalTablet(models.Model):
tab_name = models.CharField(max_length=100)
tab_image = models.ImageField(upload_to='image/', blank=True, null=True)
tab_dose_strength = models.CharField(max_length=100)
tab_pack = models.CharField(max_length=100)
tab_form = models.CharField(max_length=200)
tab_dose = models.CharField(max_length=100, default='Direction By Physician')
tab_storage = models.CharField(max_length=100)
tab_usage = models.CharField(max_length=100)
tab_brandname = models.CharField(max_length=100)
tab_composition = models.CharField(max_length=100)
def __str__(self):
return self.tab_name
浏览器显示
ValueErrorat /tablet/1/
First argument to get_object_or_404() must be a Model, Manager, or QuerySet, not 'function'.
Request Method:
GET
Request URL:
http://127.0.0.1:8000/tablet/1/
Django Version:
v2.2.1
Exception Type:
ValueError
Exception Value:
First argument to get_object_or_404() must be a Model, Manager, or QuerySet, not 'function'.
答案 0 :(得分:2)
就像错误所示一样,您试图将一个函数而不是查询集或模型传递给get_object_or_404(...)
。我认为除非您希望之前对其进行过滤,否则您想使用PharmaceuticalTablet.objects.all()
。
def alltablet(request):
tablet = PharmaceuticalTablet.objects
return render(request, 'tablets.html', {'tablet': tablet})
def tabdetail(request, tab_id):
tabdetail = get_object_or_404(PharmaceuticalTablet.objects.all(), pk=tab_id)
return render(request, 'tab_detail.html', {'tablet': tabdetail})
答案 1 :(得分:0)
tabdetail = get_object_or_404(PharmaceuticalTablet,pk = tab_id)
进行更改。您要确定模态,而不是函数。