ValueError: The view **** didnt return an HttpResponse object. It returned None instead

2024/11/16 1:53:21

I'm using Django forms to handle user input for some point on my Django app. but it keeps showing this error whenever the user tries to submit the form.

ValueError: The view *my view name goes here*  didn't return an HttpResponse object. It returned None instead

Here's the code:

Forms.py

class sendBreachForm(forms.Form):"""Form for sending messages to users."""text = forms.CharField(max_length=100)image = forms.FileField()cords = forms.CharField(widget=forms.TextInput(attrs={"type":"hidden"}))

views.py

@login_required
def web_app(request):if request.user.is_staff or request.user.is_superuser:return redirect('/ar/system/')else:if request.method == "POST":form = sendBreachForm(request.POST)print("AAAAAAAAa in a post request")if form.is_valid():print("AAAAAAAAa form is valid")text = form.cleaned_data['text']image = form.cleaned_data['image']cords = form.cleaned_data['cords']try:new_breach = Breach.object.create(text=text,image=image)add_form_cords_to_breach(request,new_breach,cords)   print("AAAAAAAA added breach")return render(request,"web_app.html",context)         except :print("AAAAAAAA error ")return render(request,"web_app.html",context)   # raise Http404('wrong data')else:form = sendBreachForm()context = {}context['form']=formcontext['all_elements'] = WaterElement.objects.all()current_site = Site.objects.get_current()the_domain = current_site.domaincontext['domain'] = the_domainall_layers = MapLayers.objects.all()context['all_layers']=all_layersreturn render(request,"web_app.html",context)

HTML

                  <form method ='post'>{% csrf_token %}{{form.text}}<label for="text">وصف المعاينة</label>{{form.image}}<label for="image">صورة المعاينة</label>{{form.cords}}<input type="submit" value = "إرسال المعاينة"></form>
Answer

The error makes complete sense, the view should return some response in all the conditions, currently you have both if and else condition for everything, except if form.is_valid() so also maintain in that.

@login_required
def web_app(request):if request.user.is_staff or request.user.is_superuser:return redirect('/ar/system/')else:if request.method == "POST":form = sendBreachForm(request.POST)print("AAAAAAAAa in a post request")if form.is_valid():print("AAAAAAAAa form is valid")text = form.cleaned_data['text']image = form.cleaned_data['image']cords = form.cleaned_data['cords']try:new_breach = Breach.object.create(text=text,image=image)add_form_cords_to_breach(request,new_breach,cords)   print("AAAAAAAA added breach")return render(request,"web_app.html",context)         except:print("AAAAAAAA error ")return render(request,"web_app.html",context)   # raise Http404('wrong data')else:print("form is not valid")messages.error(request,"form is not valid, kindly enter correct details")return redirect("some_error_page")else:form = sendBreachForm()context = {}context['form']=formcontext['all_elements'] = WaterElement.objects.all()current_site = Site.objects.get_current()the_domain = current_site.domaincontext['domain'] = the_domainall_layers = MapLayers.objects.all()context['all_layers']=all_layersreturn render(request,"web_app.html",context)
https://en.xdnf.cn/q/119586.html

Related Q&A

Game Development in Python, ruby or LUA? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Problem with this error: (-215:Assertion failed) !ssize.empty() in function cv::resize OpenCV

I got stuck with this error after running resize function line: import cv2 import numpy as np import matplotlib.pyplot as pltnet = cv2.dnn.readNetFromDarknet(yolov3_custom.cfg, yolov3_custom_last.weigh…

When I run it tells me this : NameError: name lock is not defined?

• Assume that you have an array (data=[]) containing 500,000 elements and that each element has been assigned a random value between 1 and 10 (random.randint(1,10)) .for i in range (500000):data[i]…

Unable to find null bytes in Python code in Pycharm?

During copy/pasting code I often get null bytes in Python code. Python itself reports general error against module and doesnt specify location of null byte. IDE of my choice like PyCharm, doesnt have c…

remove single quotes in list, split string avoiding the quotes

Is it possible to split a string and to avoid the quotes(single)? I would like to remove the single quotes from a list(keep the list, strings and floats inside:l=[1,2,3,4.5]desired output:l=[1, 2, 3, …

Image Segmentation to Map Cracks

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my b…

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…