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.
Related
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()
# …
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
I'm learning Django and building a page. I get the TemplateDoesNotExist error. I don't know how to fix this, But I don't know which parts of the code to show.
TemplateDoesNotExist at /
Leaning_logs/base.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 2.2.3
Exception Type: TemplateDoesNotExist
Exception Value:
Leaning_logs/base.html
Exception Location: D:\learning-note\ll_env\lib\site-packages\django\template\backends\django.py in reraise, line 84
Python Executable: D:\learning-note\ll_env\Scripts\python.exe
Python Version: 3.7.3
Python Path:
['D:\\learning-note',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\qingting_bailihua\\AppData\\Local\\Programs\\Python\\Python37',
'D:\\learning-note\\ll_env',
'D:\\learning-note\\ll_env\\lib\\site-packages']
Server time: Thu, 1 Aug 2019 10:45:39 +0000
myapp/views.py
class ContractsView(TemplateView):
def get(self, request):
return render(request, 'yourPage.html')
In the urls.py in your project folder add the url to your view.py class from application folder.
project/urls.py
urlpatterns = [
.
path('contractList/', ContractsView.as_view(), name='contractsList'),
.
]
If I did not manage to help you. Here you will find more answers:
Django TemplateDoesNotExist?
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.
Guys,
I Started playing with django-nonrel, and am trying to run my old django app on it.
My app was done on django 1.2.1 & python 2.6.
When i fire up the local server, there are a couple of warnings but the server starts well.
But when i try to run the main page from the browser, django throws the following error
ImportError at /bytex
No module named bytexpro.bytex.models
Request Method: GET
Request URL: http://127.0.0.1:8000/bytex
Django Version: 1.3 beta 1
Exception Type: ImportError
Exception Value:
No module named bytexpro.bytex.models
Exception Location: E:\home\web\bytex\bytexpro\bytex\admin.py in <module>, line 1
Python Executable: C:\python26\python.exe
...
My admin.py looks like this
from bytexpro.bytex.models import ItemGroup
from bytexpro.bytex.models import Item
from bytexpro.bytex.models import Supplier
....
Anything i i have forgotten to include?
Gath
try
from bytex.models import ....