django - “DeferredAttribute”对象不可调用
问题描述
我正在做一个简单的 django 项目,但是我遇到了你在下面看到的这个问题,所以我想要一个解决方案,因为我真的很累
我希望该代码的输出是 numberone,但实际输出是这个错误:
TypeError at /db/
'DeferredAttribute' object is not callable
Request Method: GET
Request URL: http://127.0.0.1:8000/db/
Django Version: 2.1.5
Exception Type: TypeError
Exception Value:
'DeferredAttribute' object is not callable
Exception Location: /home/mohammed/Desktop/New life programming/python /pythonDjano/Filecorse/NewDjango/MohammedAlaa/Abdullah/views.py in db, line 16
Python Executable: /usr/bin/python3
Python Version: 3.6.8
Python Path:
['/home/mohammed/Desktop/New life '
'programming/python/pythonDjano/Filecorse/NewDjango/MohammedAlaa',
'/usr/local/lib/python3.6/dist-packages/pip-19.1.1-py3.6.egg',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/mohammed/.local/lib/python3.6/site-packages',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Sat, 31 Aug 2019 11:13:18 +0000
视图.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Product
def Home(request):
return HttpResponse('Welcome, Mohammed Alaa :)')
def htm(request):
return render(request,'htm.html',{'hello':"hello Mohammed Abo Alaa again:)", 'days':['wed', 'fri', 'thru']
})
def others(request):
return HttpResponse('Welcome, Mohammed Alaa :) form others')
def db(request):
dat = ''
p1 = Product(namee='numberone', pricee=500,Type='m')
p1.save()
dat = Product.namee()
return HttpResponse(dat)
解决方案
您需要引用刚刚创建的 Product 实例,而不是整个 Product 类。而且它只是一个字符串,不需要调用它。
dat = p1.namee
推荐阅读
- python - For loop in Jinja2 splitting the chars in nested list, instead of returning whole of nested list
- opencv - 如何选择负样本?(OpenCV,物体检测)
- angular - 搜索字段并重定向到所有路线
- javascript - How to show tooltip with jsx content inside it-Reactjs
- ansible - 如何在远程机器上的文件中逐行读取
- swift - Segue 从选定的集合视图单元发送到错误的集合视图
- linux - 在脚本中打印 /etc/hosts 条目的地址
- php - 使用 fputcsv 时无法删除双引号
- php - 如何使用 PDO 修复我的密码参数
- android - 出现下拉菜单时如何将 Spinner 滚动到特定位置?