2015-02-12 191 views
0

有人可以給我一個快速的解釋,當我調用about.objects.all()和cv.objects.all()時我做錯了。我在shell中測試了它們,並得到了相同的錯誤。AttributeError:'function'對象沒有屬性'objects'

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

您的視圖的名稱和型號的名稱都cv。您需要將您的視圖名稱更改爲其他名稱。 (也許get_all_resumes

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

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

當然,知道這很簡單。謝謝ozgurv。 – SkillSet12345 2015-02-12 07:22:31

2

除了ozgurv的回答,您還import resume.models as resume_models,然後可以參考CV類是這樣的:resume_models.cv.objects.all()。我不認爲這是一個更好的解決方案,但值得一提。