'DeferredAttribute' object is not callable - django

I'm working on a simple django project, but i face this problem that you see below so, i want a solution because i really got tired
I expect the output of that code to be numberone but the actual output is this error :
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
views.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)

You need to refer to the Product instance you just created, not the whole Product class. And it's just a string, no need to call it.
dat = p1.namee

Related

What did mean by AttributeError at /login_user/?

My target is to get all the products a user added to the cart, that's why I decided to fetch the ShopingCart model from the context processor. And I added it to the context processor, and it worked well. But the problem is when I try to log out, then I get an error. Where did the actual problem occur? 😢...
models.py:
class ShopingCart(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='UserShoppingCartRelatedName',on_delete=models.CASCADE)
Product = models.ForeignKey(Products, related_name='ShoppingCartRelatedName',on_delete=models.CASCADE)
context_processors:
def ShoppingCart(request):
return {"ShoppingCart":request.user.UserShoppingCartRelatedName.all()}
error:
AttributeError at /login_user/
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Request Method: GET
Request URL: http://127.0.0.1:8000/login_user/
Django Version: 4.0.4
Exception Type: AttributeError
Exception Value:
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Exception Location: D:\1_WebDevelopment\17_Ecomerce Website\ecomerce site\env\lib\site-packages\django\utils\functional.py, line 259, in inner
Python Executable: D:\1_WebDevelopment\17_Ecomerce Website\ecomerce site\env\Scripts\python.exe
Python Version: 3.9.5
Python Path:
['D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce site',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\python39.zip',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\DLLs',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39\\lib',
'c:\\users\\dcl\\appdata\\local\\programs\\python\\python39',
'D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce site\\env',
'D:\\1_WebDevelopment\\17_Ecomerce Website\\ecomerce '
'site\\env\\lib\\site-packages']
Server time: Tue, 09 Aug 2022 11:48:23 +0000
Check if user is not none and authenticated.
def ShoppingCart(request):
if request.user is not None and request.user.is_authenticated():
return {"ShoppingCart":request.user.UserShoppingCartRelatedName.all()}
else:
return {}
It looks as if you are not logged in!
You must login before you can access the user atrubute.
If you are not loged in it says you are 'AnonymousUser'
'AnonymousUser' object has no attribute 'UserShoppingCartRelatedName'
Login and then try it!!

AttributeError at /BRMapp/view-books/

I am developing Book Record Management system as my first project on django but after performing all my functionalities I got a problem in my views.py file in a function name view-book in a line(books=models.Book.objects.all()).Although i have made a class name 'Book' in models.py and i also have imported models in my views.py file with a line(form django.db import models).Although it is not working.Kindly help
Error showing like this:::
module 'django.db.models' has no attribute 'Book'
Request Method: GET
Request URL: http://localhost:8000/BRMapp/view-books/
Django Version: 3.2.5
Exception Type: AttributeError
Exception Value:
module 'django.db.models' has no attribute 'Book'
Exception Location: G:\Django projects\firstproject\BRMapp\views.py, line 41, in viewBooks
Python Executable: C:\Users\amann\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.2
Python Path:
['G:\\Django projects\\firstproject',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\amann\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Thu, 12 Aug 2021 10:19:08 +0000
Based on the exception models points to the models submodule of the django.db module, but there is no Book defined in that module.
Likely the Book originates from elsewhere, from an appname.models module.
You thus import this with:
from appname.models import Book
# …
def viewBooks(request):
# …
# ↓ no models.
books = Book.objects.all()
# …

How do I fix this error in Python Django involving request.user.is_authenticated() and bool object not callable?

I am trying to make profile pages for each user. I added a code that checks if the user is logged in and does a redirect (see line 12 of the code below).
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect, HttpResponse
from .models import Account, ForSale, WTB
from mysite.forms import MyRegistrationForm
def signup(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/user/')
else:
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/user/')
context = {}
context.update(csrf(request))
context['form'] = MyRegistrationForm()
return render(request, 'signup.html', context)
def index(request):
return render(request, 'index.html')
However, upon accessing /signup/ on the site I get the following debug message:
TypeError at /signup/
'bool' object is not callable
Request Method: GET
Request URL: http://url:8000/signup/
Django Version: 2.0
Exception Type: TypeError
Exception Value:
'bool' object is not callable
Exception Location: /www/mysite.com/mysite/views.py in signup, line 13
Python Executable: /usr/bin/python3
Python Version: 3.5.2
Python Path:
['/www/mysite.com',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/usr/lib/python3.5/site-packages',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Sun, 3 Dec 2017 18:07:54 -0800
In older versions of Django request.user.is_authenticated was a method. It's now an attribute and no longer requires parenthesis. If you change your code to:
if request.user.is_authenticated:
It should be work as expected.
For more info see the docs here: https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated
you forget to import csrf module please try to add this line and make sure to avoid hardcoded urls try to use url names
from django.core.context_processors import csrf

Using django name 'HttpResponse' is not defined

So brand new to Python, trying to get past this django error:
NameError at /hello/
name 'HttpResponse' is not defined
Request Method:
GET
Request URL:
http://localhost:61892/hello/
Django Version:
1.11.7
Exception Type:
NameError
Exception Value:
name 'HttpResponse' is not defined
Exception Location:
c:\users\mblaylock\source\repos\mysite\mysite\mysite\views.py in hello, line 6
Python Executable:
c:\users\mblaylock\source\repos\mysite\mysite\env\Scripts\python.exe
Python Version:
3.6.2
Python Path:
['c:\\users\\mblaylock\\source\\repos\\mysite\\mysite',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env\\Scripts\\python36.zip',
'C:\\Program Files\\Python36\\DLLs',
'C:\\Program Files\\Python36\\lib',
'C:\\Program Files\\Python36',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env',
'c:\\users\\mblaylock\\source\\repos\\mysite\\mysite\\env\\lib\\site-packages']
Server time:
Mon, 13 Nov 2017 18:09:39 +0000
My code is:
class views(object):
"""description of class"""
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
Using Visual Studio for my IDE on Windows 10, if that matters.
Thanks in advance!
There's no reason to have that class views line there. Remove it.
Best approach in this situation is use single method to handle this request(you don't need any class based views here) which shouldn't be inside any class:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
If you want to use class based views you need to remember that
when you are importing something in class body it can be inaccessible in method. You should insert import at the top of python file or directly in method.

I am using django haystack with whoosh , But there some error after entering some search query in searchbar, below is the description

Error screenshot
AttributeError at /search/
'NoneType' object has no attribute '_default_manager'
Request Method: GET
Request URL: http://127.0.0.1:8000/search/?q=desktop
Django Version: 1.9
Exception Type: AttributeError
Exception Value:'NoneType' object has no attribute '_default_manager'
Exception Location: /home/ankit/venv/django/lib/python3.4/site-packages/haystack/query.py in post_process_results, line 219
Python Executable: /home/ankit/venv/django/bin/python
Python Version: 3.4.3
Python Path: ['/home/ankit/venv/django/p2',
'/home/ankit/venv/django/lib/python3.4',
'/home/ankit/venv/django/lib/python3.4/plat-x86_64-linux-gnu',
'/home/ankit/venv/django/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/ankit/venv/django/lib/python3.4/site-packages']
Server time: Sun, 27 Dec 2015 10:29:28 +0000
My search_indexes.py
import datetime
from haystack import indexes
from inventory.models import Item
class ItemIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
pub_date = indexes.DateTimeField(model_attr='pub_date')
content_auto = indexes.EdgeNgramField(model_attr='title')
def get_model(self):
return Item
def index_queryset(self, using=None):
return self.get_model().objects.all()
My settings.py file
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
},
}
The django.db.model.get_model is not available any longer in 1.9, so None is returned when calling django.db.models.get_model, but in the more recent commit (from the 3rd of January) the utils.app_loading.py is used to either use the django.apps.apps.get_model when using Django 1.7 or higher, otherwise the old django.db.models._get_models is used.
So, best to upgrade to the latest development version git+https://github.com/django-haystack/django-haystack.git.