Error The current path, didn't match any of these

Viewed 4206

I'm trying to save the data submitted on a form, and redirect the user to a results.html.

------- Front template -------------------

Welcome to readability test V1

<form action="/addtext" method="POST">{% csrf_token %}
    <input type="text" name="title"/>
    <input type="text" name="content"/>
    <input type="submit" value="Add"/>
</form>

---------- URLS.PY -----------------

from django.contrib import admin
from django.urls import path
from readtest.views import readtest, addtext


urlpatterns = [
    path('admin/', admin.site.urls),
    path('readability-test/', readtest),
    path('results/', addtext),
enter code here

------------ VIEWS.PY ------------------------

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import TestedText
# Create your views here.

def readtest(request):
    return render(request, 'test.html')


def addtext(request):
    a = TestedText(title=request.POST['title'], content=request.POST['content'])
    a.save()
    return HttpResponseRedirect('/results/')

------ ERROR IM GETTING ------------------ Page not found (404) Request Method: POST Request URL: http://127.0.0.1:8000/addtext Using the URLconf defined in redability.urls, Django tried these URL patterns, in this order:

admin/ readability-test/ results/ The current path, addtext, didn't match any of these.

1 Answers

Update your urls.py file line path('results/', addtext) to path('addtext/', addtext)

from django.contrib import admin
from django.urls import path
from readtest.views import readtest, addtext


urlpatterns = [
        path('admin/', admin.site.urls),
        path('readability-test/', readtest),
        path('addtext/', addtext),
Related