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
urls.py? where is the path inactionof your form?urls.py. And I'm not sure what to put in theaction=""for the form. Should it be{% url 'heights:success' %}or something like that?