ValueError at /mascota/crear/ The view Refugio.mascota.views.Mascota_View didn't return an HttpResponse object. It returned None instead. Request Method: GET Request URL: http://127.0.0.1:8000/mascota/crear/ Django Version: 4.0.6 Exception Type:

  • urls
    urlpatterns = [
    path(‘admin/’, admin.site.urls),
    path(‘mascota/’, include(‘Refugio.mascota.urls’)),
    path(‘adopcion/’, include(‘Refugio.adopcion.urls’)),
    ]

  • urls mascota
    urlpatterns = [
    path(‘’, index),
    path(‘crear/’, Mascota_View, name=‘mascota_creada’),
    ]

on View
def Mascota_View(request):
if request.method == ‘POST’:
form = MascotaForm(request.POST)
if form.is_valid():
form.save()
return redirect(‘index’)
else:
form = MascotaForm()
return render(request, ‘mascota/mascota_form.html’, {‘form’: request(form)})

the Message is:

ValueError at /mascota/crear/

The view Refugio.mascota.views.Mascota_View didn’t return an HttpResponse object. It returned None instead.

Request Method: GET
Request URL: http://127.0.0.1:8000/mascota/crear/
Django Version: 4.0.6
Exception Type: ValueError

Your view code:

def Mascota_View(request):
    if request.method == 'POST':
        form = MascotaForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('index')
        else:
            form = MascotaForm()
        return render(request, 'mascota/mascota_form.html', {'form': request(form)})

Notice that it only returns render(......) if request.method == 'POST'.

What does this function do for GET requests? It returns None because
execution falls off the bottom of the function. You need to return a
suitable response or raise a suitable exception.

Notice that your test is using a GET method, not a POST:

# ValueError at /mascota/crear/
The view Refugio.mascota.views.Mascota_View didn't return an 
HttpResponse object. It returned None instead.
>Request Method:|GET|
> --- | --- |
>Request URL:|http://127.0.0.1:8000/mascota/crear/|

Cheers,
Cameron Simpson cs@cskk.id.au

Thnks will check