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 is my 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']=form
context['all_elements'] = WaterElement.objects.all()
current_site = Site.objects.get_current()
the_domain = current_site.domain
context['domain'] = the_domain
all_layers = MapLayers.objects.all()
context['all_layers']=all_layers
return 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>
CodePudding user response:
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']=form
context['all_elements'] = WaterElement.objects.all()
current_site = Site.objects.get_current()
the_domain = current_site.domain
context['domain'] = the_domain
all_layers = MapLayers.objects.all()
context['all_layers']=all_layers
return render(request,"web_app.html",context)