首页 > 解决方案 > “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)

标签: django

解决方案


您需要引用刚刚创建的 Product 实例,而不是整个 Product 类。而且它只是一个字符串,不需要调用它。

dat = p1.namee

推荐阅读