1

I am new to Django and I've been trying to develop a simple site that asks the user for their email address and height. It then saves it in the database and sends an email to the user and redirects them to a page saying that it was successful.

Now the problem is whenever I press 'Submit', I get a HTTP 405 method not allowed error.

# urls.py
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    #url(r'^success/$', views.SuccessView.as_view(), name='success'),
]


# forms.py class HeightForm(forms.ModelForm):

    class Meta:
        model = Height
        fields = ['email', 'height']


# views.py
class IndexView(generic.ListView):
    form_class = HeightForm
    template_name = 'heights/index.html'

    def get_queryset(self):
        return Height.objects.all()

class HeightFormView(View):
    form_class = HeightForm
    template_name = 'heights/success.html'

    def get(self, request):
        form = form_class(None)

    def post(self, request):
        print('a' * 1000)
        form = form_class(request.POST)

        if form.is_valid:
            email = form.cleaned_data['email']
            height = form.cleaned_data['height']

            form.save()

            return HttpResponseRedirect(template_name)

    #render(request, template_name, {'form': form})


# index.html
{% extends 'heights/base.html' %}

{% block body %}
    <h1>Collecting Heights</h1>
    <h3>Please fill the entries to get population statistics on height</h3>
    <form action="" method="post">
        {% csrf_token %}
        <input type="email" name="email" placeholder="Enter your email address" required="true"/><br />
        <input type="number" min="50" max="300" name="height" placeholder="Enter your height in cm" required="true" /><br /><br />
        <input type="submit" name="submit" />
    </form>

    <a href="#">Click to view all heights in database</a>
{% endblock body %}

The code doesn't even get to the print('a' * 1000) line without generating an error. Chrome just goes to a This page isn't working page and displays HTTP ERROR 405.

I have Googled this error, but haven't found anthing helpful. Any help is appreciated

Thanks

3
  • where is ur route in urls.py ? where is the path in action of your form? Commented Jan 25, 2018 at 19:34
  • @Prakash Palnati updated code to add urls.py. And I'm not sure what to put in the action="" for the form. Should it be {% url 'heights:success' %} or something like that? Commented Jan 25, 2018 at 19:38
  • add a route for your form to get submitted in urls.py and use the same in action. should work fine. Commented Jan 25, 2018 at 19:41

2 Answers 2

2

You don't seem to have any URL defined for the HeightFormView. The form is rendered by the IndexView and posts back to itself; that view does not allow a POST method.

You would need to define a URL for HeightFormView and refer to it in the action via the {% url %} tag.

Sign up to request clarification or add additional context in comments.

Comments

1

Add a route for your form to get submitted in urls.py and use the same in action. should work fine.

urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^saveForm/$', views.HeightFormView.as_view(), name='form'), ]

And in your html form,

<form action="/saveForm" method="post">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.