0

i am getting a error: AttributeError at / 'function' object has no attribute 'objects' Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.2.5 Exception Type: AttributeError Exception Value:
'function' object has no attribute 'objects' Exception Location: G:\PYTHON DJANGO 2021 - COMPLETE COURSE\first_django_project\devsearch\project\views.py, line 27, in projects Python Executable: C:\ProgramData\Anaconda3\envs\djangoenv\python.exe Python Version: 3.9.6 Python Path:
['G:\PYTHON DJANGO 2021 - COMPLETE COURSE\first_django_project\devsearch', 'C:\ProgramData\Anaconda3\envs\djangoenv\python39.zip', 'C:\ProgramData\Anaconda3\envs\djangoenv\DLLs', 'C:\ProgramData\Anaconda3\envs\djangoenv\lib', 'C:\ProgramData\Anaconda3\envs\djangoenv', 'C:\ProgramData\Anaconda3\envs\djangoenv\lib\site-packages'] Server time: Sat, 21 Aug 2021 13:18:04 +0000

# models.py
from django.db import models
import uuid


# Create your models here.
class Project(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True,blank=True)
    demo_link = models.CharField(max_length=2000,null=True,blank=True)
    source_link = models.CharField(max_length=2000,null=True,blank=True)
    tag = models.ManyToManyField('Tag',blank=True)
    vote_total = models.IntegerField(default=0,null=True,blank=True)
    vote_ratio = models.IntegerField(default=0,null=True,blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4,unique=True,primary_key=True,editable=False)
    
    def __str__(self):
        return self.title


# views.py
from django.shortcuts import render
from .models import Project


# Create your views here.
def projects(requests):
    projects = Project.objects.all()
    return render(requests,"project/projects.html", {'projects':projects})
2
  • Did you define a view with the name Project in your file? Commented Aug 21, 2021 at 13:30
  • I suggest you to proper format your question to make other help you with this issue. Good luck. Commented Aug 21, 2021 at 22:35

2 Answers 2

2

You likely defined a function in your views named Project, as a result the reference to the Project model is altered to the view function.

You can define a view function name in lowercase, so project instead of Project, and thus implement this as:

from django.shortcuts import render
from .models import Project

# Create your views here.
def projects(requests):
    projects = Project.objects.all()
    return render(requests, 'project/projects.html', {'projects':projects})

# project instead of Project
def project(request, pk):
    # …
Sign up to request clarification or add additional context in comments.

Comments

0

try migrating the database

python manage.py makemigrations
python manage.py migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.