AttributeError:'功能'对象没有属性'对象'

时间:2015-02-12 07:10:57

标签: python django django-views

有人可以在调用about.objects.all()和cv.objects.all()时快速解释一下我做错了什么。我在shell中测试了它们并得到了同样的错误。

from django.shortcuts import render
from resume.models import websites, about, cv
from django.http import HttpResponse


def about_text(request):
    about_text = about.objects.all()
    context_dict = {'text': about_text}

    response = render(request, 'resume.html', context_dict)
    return response 



def cv(request):
    position = cv.objects.all()
    context = {'job': position}

    response = render(request, 'resume.html', context_dict)
    return response

模型

from django.db import models
from django.utils import timezone

class cv(models.Model):
    title = models.CharField(max_length=128)
    company = models.CharField(max_length=128)
    started = models.DateField()
    ended = models.DateField()
    roles = models.TextField()

    def __unicode__(self):
        return self.position


class about(models.Model):
    text = models.TextField()

    def __unicode__(self):
        return self.text

追溯错误

Request Method: GET
Request URL: http://127.0.0.1:8000/resume/test

Django Version: 1.7.3
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'resume',
 'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\WriteCode\test_env\portfolio\resume\views.py" in cv
  16.   position = cv.objects.all()

Exception Type: AttributeError at /resume/test
Exception Value: 'function' object has no attribute 'objects'

2 个答案:

答案 0 :(得分:2)

您的观点的名称和型号名称均为cv。您需要将视图的名称更改为其他名称。 (也许是get_all_resumes

def get_all_resumes(request):
    position = cv.objects.all()
    context = {'job': position}

    response = render(request, 'resume.html', context)
    return response

答案 1 :(得分:2)

除了ozgurv的回答,你还可以import resume.models as resume_models然后像这样引用cv类:resume_models.cv.objects.all()。我不认为这是一个更好的解决方案,但值得一提。