Django Tutorial: name 'HttpResponse' is not defined - python

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:
Since I am new to Django, I do not know what is going on. Please help.

from django.http import HttpResponse
in your views file at the top

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

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.

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

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.

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.

For me it was because I used singe quotes (') instead of double quotes (")

Check your import statement.
Check your function. I had "HttpsResponse" instead of "HttpResponse"
Good luck.

Related

How can i solve syntax error during making webapp?

im working with django and making webapp now this has occurred
def Notes(generic.DetailView):
^
Error invalid syntax
Function name:NotesDetaiView
It should look like this. Check the documentation.
from django.views.generic.detail import DetailView
from .models import Notes # or other location
class NotesDetailView(DetailView):
model = Notes
# rest of code

Why not all showing up in vs code django/python intellisense

Hi I am new to Python/django and am using VS Code. Now I got python IntelliSense and pylance extension installed and most things are showing up on IntelliSense but some aren't.
How can I get it to work for everything?
I would very much appreciate some insight since it's driving me nuts...
request.POST from an imported lib not showing up
selected_meetup.participants not showing up nor is selected_meetup.participants.add
from urllib import request
from django.forms import SlugField
from django.shortcuts import render
from .models import Meetup
from .forms import RegistrationForm
# from django.http import HttpResponse
# Create your views here.
def index(request):
meetups = Meetup.objects.all()
return render(request, 'meetups/index.html', {
'meetups': meetups
})
def meetup_details(request, meetup_slug):
try:
selected_meetup = Meetup.objects.get(slug=meetup_slug)
if request == 'GET':
registration_form = RegistrationForm()
else:
registration_form = RegistrationForm(request.POST)
registration_form = RegistrationForm(request.)
if registration_form.is_valid():
participant = registration_form.save()
selected_meetup.participants.add(participant)
return render(request, 'meetups/meetup-detail.html', {
'meetup_found': True,
'meetup': selected_meetup,
'form': registration_form
})
except Exception as exc:
return render(request, 'meetups/meetup-detail.html', {'meetup_found': False
})
Update:
That's great guys, I now understand the type problem it's obvious now (only had so many extensions issues especially django/html) I got confused...
Now I added types to Meetup and Participant but selected_meetup.participants. still refused to show me its method ADD.
I assume the problem is the lib is written without types. Is there a way to solve that? because quite valuable to me to be able to see what's possible write away...
have you got solutions to that?
I would re-install the extensions, then make sure it is referencing the right version of python. If that doesn't work I would recommend to just type it out in full and look past the error.
You didn't declare what type of object request is in the programming process. Maybe you can declare request = request () in the code.The second is the same. vscode can't recognize the type of the selected_meetup you wrote.
You can hover over these two objects and find that the vscode prompt the type can be any.

How does a django render function know where this file is located?

Right so I am following a long a python Django tutorial and I am confused on how this
render function knows where index.html is. The file I am in is views.py.
from django.shortcuts import render
from appTwo.forms import NewUserForm
def index(request):
return render(request,'appTwo/index.html')
The file structure looks like
It doesn't "know" per se, but it does scan all template directories you list in settings.py as described here. The first that matches the name you provide is used.

Saving data to Postgres DB using django rest framework

I'm new in django development. I m developing an API in which i am sending data from client side (mobile app) and this data is to be stored in a database using django. And if I query the data the data should be fetched from the DB. Database is preferably postgres/mysql DB. I have written some part of the code but stuck how to proceed. I'll appreciate if someone can guide me how to proceed.
from django.shortcuts import render
from rest_framework.views import APIView
from django.http import Http404
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import json
# Create your views here.
#api_view(["POST"])
def getIdealWeight(heightData):
try:
height=json.loads(heightData.body)
weight=str(height*10)
return JsonResponse("the ideal weight is:"+weight+" kg.",safe=False)
except ValueError as e:
return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
Judging from the code, I'll advice you start from the very beginning of DRF Tutorials and cover a great part of it before going further with your project. After the quickstart, you should visit the individual parts dedicated to the major concepts like serializers, views, etc. Jumping straight to the project without having a fair knowledge of the framework will make it difficult to achieve what you want.

Django often returns outdated view

I'm pretty new to web development (not to programming), but I just successfully (sort of) deployed a really basic hello-world style Django app. The first time I did it, I had an issue in my HTML. Here's my whole view with the error:
from django.http import HttpResponse
import datetime
def homepage(request):
now=datetime.datetime.now()
html="<html><body><It is now %s.</body></html>" % now
return HttpResponse(html)
The extra < just after the first body tag caused the browser to display a blank page. I figured out what I did and fixed the error. I also added a title so I'd be able to track what's going on (somewhat) better. The old view became this:
from django.http import HttpResponse
import datetime
def homepage(request):
now=datetime.datetime.now()
html="<html><head><title>Hello</title></head><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Now the browser displays the old view (a blank page) most of the time, just the title with a blank body sometimes, and occasionally the whole correct new view. I have no clue what's going on. I'm running nginx with flup to handle the FastCGI. Ideas?
After changing your code you need to restart your FastCGI server, not Nginx.

Categories