Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Form Submission is not Working

app/2/book/POST, didn't match any of these.

Getting this error when I click on Submit button

#views.py

def book(request,doctor_id):
    try:
        doc = doctor.objects.get(pk=doctor_id)
    except doctor.DoesNotExist:
        raise Http404("Doctor does not exists and therefore unable to book")

    form = forms.BookingForm()
    context = {
        'doc': doc,
        'form': form
    }

    if request.method == 'POST':
        form = forms.BookingForm(request.POST)
        if form.is_valid():
            print("Good Form")


    return  render(request,'app/book.html',context)
<!-- HTML Template -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>{{ doc.name }}</h1>
<br>
<h2>Speciality : {{ doc.speciality }}<br><br>
Fees : {{ doc.fees }}
</h2>

<a href="/app/{{ doc.id }}/book"> <button type="button" onclick="/app/{{ doc.id }}/book">Book Appointment</button></a>

</body>
</html>
#urls.py
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$',views.index,name='index'),
url(r'^(?P<doctor_id>[0-9]+)/book/$', views.book, name='book'),
    url(r'^(?P<doctor_id>[0-9]+)/$', views.detail, name='detail'),

]

2 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hey Pravandan Chand

So I see the issue you are having, inside your app/templates/app/. You have a file called **book.html*

<form method="POST" action="POST">

    {% csrf_token %}
    {{ form.as_p }}



        <input type="submit">
</form>

method should be POST or GET.

action should be the URL of where you want to POST your form to.

which is why you get the app/2/book/POST ... POST at the end.

so give this a shot, replace your form inside book.html with this

<form method="POST" action="{% url 'book' doc.id %}">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit">
</form>

Thank you very much Chris

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hey Pravandan Chand

Since I dont have all your files to test myself, Could you try something?

In your HTML file could you place a / at the end of the URL href and onclick. Like so...

<a href="/app/{{ doc.id }}/book/"> <button type="button" onclick="/app/{{ doc.id }}/book/">Book Appointment</button></a>

or you could remove that END slash from the URLS even to try it that way. With these files its the only thing Im noticing.

Please find the entire source code at https://github.com/Pravandan/healthfun