Name httpresponse is not defined django ошибка

I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).

I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:

python manage.py runserver

I get the following error:
enter image description here

Since I am new to Django, I do not know what is going on. Please help.

asked May 25, 2017 at 16:24

SumNeuron's user avatar

1

from django.http import HttpResponse

in your views file at the top

answered May 25, 2017 at 16:27

Exprator's user avatar

ExpratorExprator

27k6 gold badges48 silver badges60 bronze badges

0

Put this import in your poll/views.py before using HttpResponse.

from django.http import HttpResponse

answered May 25, 2017 at 16:29

khue bui's user avatar

khue buikhue bui

1,3763 gold badges22 silver badges31 bronze badges

0

from django.http import HttpResponse
add this line on the top of polls/views.py file. I am new too, and had the same error. good Luck and i see u around.

answered Jun 7, 2017 at 2:18

Claudio Scheuermann's user avatar

in your polls/views.py

By default is :

from django.shortcuts import render

change to:

from django.shortcuts import render,HttpResponse

this will call the HttpResponse class

answered Mar 3, 2018 at 5:27

li bing zhao's user avatar

li bing zhaoli bing zhao

1,38813 silver badges12 bronze badges

In my case the import was there, but when I called HttpsResponse I called it with small h as a typo instead of the capital H

from django.http import HttpResponse
def home(request):
      return HttpResponse("Hello!") #==> This one was with httpResponse so the same error been received.

answered May 8, 2020 at 16:18

HassanSh__3571619's user avatar

1

I had imported HttpResponse and still got this error.

If you use Apache server as your primary server for web, try restarting Apache and reloading the page.

TylerH's user avatar

TylerH

20.8k66 gold badges76 silver badges101 bronze badges

answered Oct 28, 2017 at 11:51

Jerzy Drożdż's user avatar

For me it was because I used singe quotes (‘) instead of double quotes («)

answered Aug 9, 2018 at 14:00

Maxime Chevallier's user avatar

1

  1. Check your import statement.
  2. Check your function. I had «HttpsResponse» instead of «HttpResponse»

Good luck.

answered Aug 22, 2019 at 2:58

Freddy's user avatar

FreddyFreddy

1441 silver badge5 bronze badges

1

Итак, совершенно новый для Python, пытаясь преодолеть эту ошибку django:

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

Мой код:

class views(object):

    """description of class"""

    from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")

Использование Visual Studio для моей среды разработки в Windows 10, если это имеет значение.

Заранее спасибо!

I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).

I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:

python manage.py runserver

I get the following error:
enter image description here

Since I am new to Django, I do not know what is going on. Please help.

asked May 25, 2017 at 16:24

SumNeuron's user avatar

1

from django.http import HttpResponse

in your views file at the top

answered May 25, 2017 at 16:27

Exprator's user avatar

ExpratorExprator

26.8k6 gold badges45 silver badges57 bronze badges

0

Put this import in your poll/views.py before using HttpResponse.

from django.http import HttpResponse

answered May 25, 2017 at 16:29

khue bui's user avatar

khue buikhue bui

1,3563 gold badges21 silver badges30 bronze badges

0

from django.http import HttpResponse
add this line on the top of polls/views.py file. I am new too, and had the same error. good Luck and i see u around.

answered Jun 7, 2017 at 2:18

Claudio Scheuermann's user avatar

in your polls/views.py

By default is :

from django.shortcuts import render

change to:

from django.shortcuts import render,HttpResponse

this will call the HttpResponse class

answered Mar 3, 2018 at 5:27

li bing zhao's user avatar

li bing zhaoli bing zhao

1,37813 silver badges12 bronze badges

In my case the import was there, but when I called HttpsResponse I called it with small h as a typo instead of the capital H

from django.http import HttpResponse
def home(request):
      return HttpResponse("Hello!") #==> This one was with httpResponse so the same error been received.

answered May 8, 2020 at 16:18

HassanSh__3571619's user avatar

1

I had imported HttpResponse and still got this error.

If you use Apache server as your primary server for web, try restarting Apache and reloading the page.

TylerH's user avatar

TylerH

20.7k65 gold badges73 silver badges98 bronze badges

answered Oct 28, 2017 at 11:51

Jerzy Drożdż's user avatar

For me it was because I used singe quotes (‘) instead of double quotes («)

answered Aug 9, 2018 at 14:00

Maxime Chevallier's user avatar

1

  1. Check your import statement.
  2. Check your function. I had «HttpsResponse» instead of «HttpResponse»

Good luck.

answered Aug 22, 2019 at 2:58

Freddy's user avatar

FreddyFreddy

1441 silver badge5 bronze badges

1

  • Py Py
  • Feb 17, 2022
NameError: name 'HttpResponse' is not defined

Solution: name ‘HttpResponse’ is not defined

# Add the following line to the top of your code
from django.http import HttpResponse

For more information:
Django HttpResponse

Search

Recent Posts

Tags

Итак, совершенно новый для Python, пытаясь преодолеть эту ошибку django:

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:usersmblaylocksourcereposmysitemysitemysiteviews.py in hello, line 6
Python Executable:
c:usersmblaylocksourcereposmysitemysiteenvScriptspython.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

Мой код:

class views(object):

    """description of class"""

    from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")

Использование Visual Studio для моей среды разработки в Windows 10, если это имеет значение.

Заранее спасибо!

Solution 1

from django.http import HttpResponse

in your views file at the top

Solution 2

Put this import in your poll/views.py before using HttpResponse.

from django.http import HttpResponse

Solution 3

from django.http import HttpResponse
add this line on the top of polls/views.py file. I am new too, and had the same error. good Luck and i see u around.

Solution 4

in your polls/views.py

By default is :

from django.shortcuts import render

change to:

from django.shortcuts import render,HttpResponse

this will call the HttpResponse class

Comments

  • I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).

    I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:

    python manage.py runserver
    

    I get the following error:
    enter image description here

    Since I am new to Django, I do not know what is going on. Please help.

  • This just reiterates the accepted answer from 2017.

  • This is a typo that other people probably won’t make. At best it’s perhaps comment material. OP also doesn’t have quotes of either type in their scenario, so this is also at best unrelated to the problem.

  • This just reiterates the accepted answer from 2017 except highlighting that you made a typo. At best it should be a comment about the command being case-sensitive, but that’s a rather generous take.

Recents

Python – Django Tutorial: name ‘HttpResponse’ is not defined

djangopython

I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).

I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:

python manage.py runserver

I get the following error:
enter image description here

Since I am new to Django, I do not know what is going on. Please help.

Best Solution

from django.http import HttpResponse

in your views file at the top

Related Solutions

Python – Calling a function of a module by using its name (a string)

Assuming module foo with method bar:

import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()

You could shorten lines 2 and 3 to:

result = getattr(foo, 'bar')()

if that makes more sense for your use case.

You can use getattr in this fashion on class instance bound methods, module-level methods, class methods… the list goes on.

Python – Getting the class name of an instance

Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I think is what you want.

>>> import itertools
>>> x = itertools.count(0)
>>> type(x).__name__
'count'

If you’re still using Python 2, note that the above method works with new-style classes only (in Python 3+ all classes are «new-style» classes). Your code might use some old-style classes. The following works for both:

x.__class__.__name__

I am learning Django via their tutorial for getting started. I have looked at other Django Tutorial errors and did not see this one (although I did not search every listing).

I have made the changes to mysite/urls.py and polls/urls.py exactly as they demonstrate and I have run the server command:

python manage.py runserver

I get the following error:
enter image description here

Since I am new to Django, I do not know what is going on. Please help.

What is Django httpresponsenotallowed?

HttpResponseNotAllowed is a subclass of HttpResponse that returns a HTTP response with 405 status. 1. Django HttpResponseNotAllowed with examples

What is httpresponse?

HttpResponse is a class thats return an http response, and in ths tutorial, we’ll learn how to use HttpResponse with simple examples. 1. HttpResponse

What is a httprequest in Django?

– Django HttpResponse Explained As we mentioned earlier, a Django view takes a web request and returns a web response. After the request is captured, Django creates an HttpRequest object with associated metadata that is then processed by middlewares and backend logic.

How to return JSON data from Django REST API?

Often, and especially if you are designing a REST API, you’ll have to return JSON data to the client. Django has a special HttpResponse subclass called JsonResponse that makes this easy for you. 3. Use HttpResponse to return data as a File attachment

from django.http import HttpResponse

in your views file at the top

Так что новенький в Python, пытающийся обойти эту ошибку django:

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

Мой код:

class views(object):

    """description of class"""

    from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")

Использование Visual Studio для моей IDE в Windows 10, если это имеет значение.

Заранее спасибо!

2017-11-13 18:13

2
ответа

Там нет причин иметь это class views линия там. Убери это.

2017-11-13 18:24

Лучший подход в этой ситуации — использовать один метод для обработки этого запроса (здесь вам не нужны представления на основе классов), который не должен быть внутри какого-либо класса:

from django.http import HttpResponse

def hello(request): 
    return HttpResponse("Hello world")

Если вы хотите использовать представления на основе классов, вы должны помнить, что при импорте чего-либо в тело класса это может быть недоступно в методе. Вы должны вставить импорт в верхней части файла Python или непосредственно в методе.

2017-11-13 18:23

Понравилась статья? Поделить с друзьями:
  • N700e ошибка е09
  • N700e ошибка e17
  • Mysql код ошибки 1146
  • N6eak7du ошибка на опель
  • Mysql код ошибки 1064