AttributeError at /customers/f0e85ace-438f-4c61-a129-6d5986e6f346/ 'customer' object has no attribute 'get'
Question:
i have just started django and i am struggling to find error .Seems like get() method is not being recognized .What should i do?I would really appreciate your help
from django.db import models
from django.core.validators import RegexValidator
import uuid
# Create your models here.
class customer(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
name=models.CharField(max_length=200,null=True)
phone_no=models.CharField(max_length=200, validators=
[RegexValidator(r'^d{1,10}$')],null=True)
email=models.EmailField(max_length=200,null=True)
def __str__(self):
return self.name
class tag(models.Model):
name=models.CharField(max_length=200,null=True)
def __str__(self):
return self.name
class products(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
categories=[
('organic','organic'),
('inorganic','inorganic')
]
name=models.CharField(max_length=200,null=True)
price=models.FloatField(null=True)
manufacturedate=models.DateTimeField(auto_now_add=True,null=True)
description:models.TextField(null=True)
categories=models.CharField(max_length=200,null=True,choices=categories)
tag=models.ManyToManyField(tag)
def __str__(self):
return self.name
class order(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
status=[
('pending','pending'),
('out of stock','out of stock',),
('Delivered',('Delivered'))
]
ordered_date=models.DateTimeField(auto_now_add=True)
status=models.CharField(max_length=200,null=True,choices=status)
customer=models.ForeignKey(customer,null=True,on_delete=models.SET_NULL)
product=models.ForeignKey(products,null=True,on_delete=models.SET_NULL)
urls.py
url seems ok i could not find any mistake
from django.urls import path
from . import views
urlpatterns=[
path('',views.home),
path('products/',views.product),
path('customers/<str:pk>/',views.customer),
]
views.py
Here in the customer it is not it says get is not the attribute .seems like i am missing something
from django.shortcuts import render
from .models import *
# Create your views here.
def home(request):
customers=customer.objects.all()
orders=order.objects.all()
total_customers=customers.count()
total_orders=orders.count()
delivered=orders.filter(status='Delivered').count()
pending=orders.filter(status='pending').count()
context={'customers':customers,'orders':orders,'total_customers':total_customers,
'total_orders':total_orders,'delivered':delivered,
'pending':pending }
return render(request,'accounts/home.html',context)
def product(request):
product=product
return render(request,'accounts/products.html')
def Customer(request,pk):
Customer=customer.objects.get(id=pk)
return render(request,'accounts/customers.html')
Answers:
You got this error because you did wrong in below code.
instead:
path('customers/<str:pk>/',views.customer),
try this:
path('customers/<int:pk>/',views.customer),
Then it will solve erro. And don’t add ID field to model because it generates automatically after migrations.
In views.py, your view is named Customer
with uppercase C
. When you write from .models import *
, it imports your model customer
with lowercase c
.
Thus, in urls.py
when you write views.customer
, this refers to the model, not the view. The view is views.Customer
:
urlpatterns=[
path('',views.home),
path('products/',views.product),
path('customers/<str:pk>/',views.Customer),
]
Note: By convention, classes should be named following UpperCamelCase
, and functions should be named following lower_snake_case
. Following these conventions will help you avoid this kind of mistakes, and help other people to read your code. See naming conventions.
i have just started django and i am struggling to find error .Seems like get() method is not being recognized .What should i do?I would really appreciate your help
from django.db import models
from django.core.validators import RegexValidator
import uuid
# Create your models here.
class customer(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
name=models.CharField(max_length=200,null=True)
phone_no=models.CharField(max_length=200, validators=
[RegexValidator(r'^d{1,10}$')],null=True)
email=models.EmailField(max_length=200,null=True)
def __str__(self):
return self.name
class tag(models.Model):
name=models.CharField(max_length=200,null=True)
def __str__(self):
return self.name
class products(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
categories=[
('organic','organic'),
('inorganic','inorganic')
]
name=models.CharField(max_length=200,null=True)
price=models.FloatField(null=True)
manufacturedate=models.DateTimeField(auto_now_add=True,null=True)
description:models.TextField(null=True)
categories=models.CharField(max_length=200,null=True,choices=categories)
tag=models.ManyToManyField(tag)
def __str__(self):
return self.name
class order(models.Model):
id=models.UUIDField(primary_key=True,default=uuid.uuid4)
status=[
('pending','pending'),
('out of stock','out of stock',),
('Delivered',('Delivered'))
]
ordered_date=models.DateTimeField(auto_now_add=True)
status=models.CharField(max_length=200,null=True,choices=status)
customer=models.ForeignKey(customer,null=True,on_delete=models.SET_NULL)
product=models.ForeignKey(products,null=True,on_delete=models.SET_NULL)
urls.py
url seems ok i could not find any mistake
from django.urls import path
from . import views
urlpatterns=[
path('',views.home),
path('products/',views.product),
path('customers/<str:pk>/',views.customer),
]
views.py
Here in the customer it is not it says get is not the attribute .seems like i am missing something
from django.shortcuts import render
from .models import *
# Create your views here.
def home(request):
customers=customer.objects.all()
orders=order.objects.all()
total_customers=customers.count()
total_orders=orders.count()
delivered=orders.filter(status='Delivered').count()
pending=orders.filter(status='pending').count()
context={'customers':customers,'orders':orders,'total_customers':total_customers,
'total_orders':total_orders,'delivered':delivered,
'pending':pending }
return render(request,'accounts/home.html',context)
def product(request):
product=product
return render(request,'accounts/products.html')
def Customer(request,pk):
Customer=customer.objects.get(id=pk)
return render(request,'accounts/customers.html')
You got this error because you did wrong in below code.
instead:
path('customers/<str:pk>/',views.customer),
try this:
path('customers/<int:pk>/',views.customer),
Then it will solve erro. And don’t add ID field to model because it generates automatically after migrations.
In views.py, your view is named Customer
with uppercase C
. When you write from .models import *
, it imports your model customer
with lowercase c
.
Thus, in urls.py
when you write views.customer
, this refers to the model, not the view. The view is views.Customer
:
urlpatterns=[
path('',views.home),
path('products/',views.product),
path('customers/<str:pk>/',views.Customer),
]
Note: By convention, classes should be named following UpperCamelCase
, and functions should be named following lower_snake_case
. Following these conventions will help you avoid this kind of mistakes, and help other people to read your code. See naming conventions.