cannot import name HttpResponse - python

views.py
from django import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
Request Method: GET
Request URL: http://127.0.0.1:8000/hello/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
cannot import name HttpResponse

You can try this: from django.http import HttpResponse

You are importing from wrong location
django.http this is right location from django.http import HttpResponse

Changing from django to django.http in the views.py file works in most cases, if it doesn't check the urls.py file in the project dir and make sure that :
the path() to your view is imported correctly
that it is above the path('admin/', admin.site.urls) in the urlpatterns list

Related

When I am defining the URL patterns in django version 2.1.4 , I am getting an error. please show me a path

I am trying to run the web application named test app.I have imported all the modules correctly and crosschecked them.The error is below with the code.
Error on running the project from CMD:
path('about/',views.about),
AttributeError: module 'testapp.views' has no attribute 'about'
URLs.py:
from django.contrib import admin
from django.urls import path
from testapp import views
urlpatterns = [
path('about/',views.about),]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def about(request):
s="<h1> This is an about page</h1>"
return HttpResponse(s)

Problem with AJAX calls in Django app while developing google chrome extension

When I am trying to send data to my localhost using the ajax call in popup.js, I am getting an error :
Not Found: /sentiment/
"GET /sentiment/?number=219 HTTP/1.1" 404 1714
Even though I checked the url and it is correct and exists.
This is the snippet of my ajax call:
$.ajax({
url:"http://127.0.0.1:8000/sentiment/",
dataType:"json",
data:{
number:newTotal
},
crossDomain:true,
success:function(json)
{
$('#total').text(json.number)
}
})
and this is my urls.py file in django app :
from django.contrib import admin
from django.urls import path,include
from DetectHate import views
from django.urls import path,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^sentiment/$', views.sentiment,name="sentiment"),
]
and this is my views.py file -
import json
from django.http import Http404,HttpResponse
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def sentiment(request):
if request.is_ajax():
var=request.POST.get('number')+5
data={}
data['number']=var+5
return HttpResponse(json.dumps(data),content_type='application/json')
else:
raise Http404

How to fix ImportError: cannot import name 'HTTpResponse' from 'django.http'

After several trials, I still get the same
ImportError: cannot import name 'HTTpResponse' from 'django.http' (/Users/mac/my_env/lib/python3.7/site-packages/django/http/__init__.py) after running '$ python manage.py runserver'
in the terminal. Can someone please enlighten me with this issue? Thanks!
The code below were originally taken from the Django tutorial on its official website.
from django.shortcuts import render
from django.http import HTTpResponse
def index(request):
return HTTpResponse("Hello, world. You're at the polls index.")
if u are using(httpresponse) this in views.py(django = 3.0.5 python = 3.8.2)
try this:
from django.shortcuts import HttpResponse
(make sure its in camel case)
hope this will help
Try this:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Python is a case sensitive language,here camelCase are used and follow this.I think,it will work
Try this:
from django.http import HttpResponse
What was the reason for the random capitalization?

Django error templates in custom path

I have dir template with all templates, and I have dir errors, with all templates with errors 404.html.
I found the following old solution:
In urls.py my app I wrote
from django.conf.urls import (handler400, handler403, handler404, handler500)
handler404 = 'my_app.views.page_not_found'
And in view my app
from django.shortcuts import (render_to_response)
from django.template import RequestContext
def page_not_found(request):
response = render_to_response(
'errors/404.html',
context_instance=RequestContext(request))
response.status_code = 404
return response
But I use Django 1.11 and this doesn't work.
If you follow the docs
You should return HttpResponseNotFound which is the same as HttpResonse except for status code. It worked for me.
def bad_request(request):
response = render_to_response(
'errors/404.html'
)
return HttpResponseNotFound(response.content)

Error 404 handler in Django 1.9.2

I have got installed Django 1.9.2 and want to use a custom view to handle error 404. I am trying to do this:
# myapp/urls.py
from django.conf.urls import url, handler404
from django.views.generic.base import RedirectView
from django.core.urlresolvers import reverse_lazy
import views
urlpatterns = [
url(r'^not-found/$', views.NotFoundView.as_view(), name='not_found'),
]
#handler404 = RedirectView.as_view(url=reverse_lazy('not_found'))
handler404 = 'myapp.views.NotFoundView.not_found_handler'
# myapp/views/NotFoundView.py
from django.http import HttpResponse
class NotFoundView(object):
#classmethod
def as_view(cls):
return cls.handler
#classmethod
def handler(cls, request):
return HttpResponse(u"My error 404", status=404)
def not_found_handler(request, exception, template_name='404.html'):
return HttpResponse(u"New handler 404", status=404)
In other words, I overrode handler404 with RedirectView initialized by the URL to my custom view called NotFoundView. But unfortunately it didn't work, I got the standard Django's error 404 instead of my custom one. What am I doing wrong?

Categories