I have a víews to display and save a form as below:
@login_required(login_url='/login') # Check login
def addlisting(request):
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
else:
form = ProductForm()
return render(request, 'listing/addlisting.html', {
'form': form
})
But When I load the html file I got this error
ValueError at /addlisting
The view listing.views.addlisting didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL: http://127.0.0.1:8000/addlisting
Django Version: 3.2.3
Exception Type: ValueError
Exception Value:
The view listing.views.addlisting didn't return an HttpResponse object. It returned None instead.
Exception Location: C:\Users\Daisy\OneDrive\Documents\Work\django\shecodes\bookapp\env\lib\site-packages\django\core\handlers\base.py, line 309, in check_response
Python Executable: C:\Users\Daisy\OneDrive\Documents\Work\django\shecodes\bookapp\env\Scripts\python.exe
Python Version: 3.8.2
Python Path:
['C:\\Users\\Daisy\\OneDrive\\Documents\\Work\\django\\shecodes\\bookapp\\bookapp',
'C:\\Users\\Daisy\\OneDrive\\Documents\\Work\\django\\shecodes\\bookapp\\env\\Scripts\\python38.zip',
'c:\\users\\daisy\\appdata\\local\\programs\\python\\python38\\DLLs',
'c:\\users\\daisy\\appdata\\local\\programs\\python\\python38\\lib',
'c:\\users\\daisy\\appdata\\local\\programs\\python\\python38',
'C:\\Users\\Daisy\\OneDrive\\Documents\\Work\\django\\shecodes\\bookapp\\env',
'C:\\Users\\Daisy\\OneDrive\\Documents\\Work\\django\\shecodes\\bookapp\\env\\lib\\site-packages']
Server time: Sun, 30 Jan 2022 07:41:40 0000
Please take a look. Thanks in advance !!!!!!!!!!!!!!!!!!!!
CodePudding user response:
Are you sure you are using POST method?
Please try it out:
@login_required(login_url='/login') # Check login
def addlisting(request):
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
else:
form = ProductForm()
return render(request, 'listing/addlisting.html', {'form': form})
return redirect('home')
NOTE: Just trying to get with the error. please just check it out that you are redirected to home. if you get to the home that means you are not using the POST method as well.
Yep that is the error: Request Method: GET
you are using GET. please send the request using POST method.
Good idea to use:
from django.views.decorators.http import require_http_methods
@require_http_methods(["POST"])
def my_view(request):
# I can assume now that only GET or POST requests make it this far
# ...
pass
please read more about using decoratos to allow the method you want on your function: https://docs.djangoproject.com/en/4.0/topics/http/decorators/
this may help you:
from django.views.decorators.http import require_http_methods
@require_http_methods(["POST"])
@login_required(login_url='/login') # Check login
def addlisting(request):
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
form = ProductForm()
return render(request, 'listing/addlisting.html', {'form': form})
CodePudding user response:
There is not else part in your views.py so while rendering page GET request is called and because of that it throws didn't return an HttpResponse object. So add else part and render your ProductForm as
@login_required(login_url='/login') # Check login
def addlisting(request):
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
else:
form = ProductForm()
return render(request, 'listing/addlisting.html', {'form': form})
else: #<------------ add else part here which will `render` your form
form = ProductForm()
return render(request, 'listing/addlisting.html', {'form': form})
