Method Not Allowed (POST)


#1

Hi,
I’m stuck with the error ‘Method Not Allowed (POST)’ while using the contact form for my site. As I’m newbie to django I can’t figure out what’s wrong with my code, so please help me with this one :)
Here is my code:
views.py :

        def contact(request) :
              form_class = ContactForm
          if request.method == 'POST': 
	       form = form_class(request.POST)

		if form.is_valid():
			first_name = request.POST.get('first_name', '')
			last_name = request.POST.get('last_name', '')
			company_name = request.POST.get('company_name', '')
			contact_email = request.POST.get('email', '')
			phone = request.POST.get('phone', '')
			form_content = request.POST.get('content', '')

    
			template = get_template('contact_template.txt')
			context = {
				'first_name': first_name,
				'last_name': last_name,
				'company_name': company_name,
				'contact_email': contact_email,
				'phone': phone,
				'form_content': form_content,
			}
			content = template.render(context)

			email = EmailMessage(
				"New contact form submission",
				content,
				"Your website" +'',
				['[email protected]'],
				headers = {'Reply-To': contact_email }
			)
			email.send()
			return redirect('contact')

	return render(request, 'index.html', {'form': form_class})

I’m using this contact form for single page site.


#2

Unfortunately I can’t tell if there are errors here because it looks like the spacing is off (and indentation is important for Python). On your end, is everything indented correctly? You can also try putting your code on pastebin.com.


#3

Yes here it is intended properly, no error of indentation.


#4

Okay, then it looks like you have indentation problems:

image

if form.is_valid(): needs to be indented in from the start of the method, for a start. Try fixing the indentation and see if the error is fixed.


#5


here is the screenshot of views.py, it is indented properly but it shows the error : Method Not Allowed (POST) when submitting the form.


#6

Thanks! That does seem in order. What about your urls.py? Are you url definitions in the correct order? This error can also occur if another url is being triggered.


#7

This is my urls.py :

urlpatterns = [

url(r'^$',views.IndexView.as_view(), name = 'index' ),
url(r'^contact/$', views.contact, name='contactus'),

]

Now I’m seeing this error:


#8

Can you show me the top part of the error? It looks like it’s cut off.


#9

I’ve mailed you the error file, can you please check it out :)


#10

The error that you emailed, An established connection was aborted by the software in your host machine — sounds like there is something else interfering. Do you have anything else running?


#11

I exactly don’t know what the problem is… but what I guess is that I’ve have static file mappings in settings.py, is that causing error?
If so how can I solve that?


#12

Unfortunately I think this question is too complicated for me to diagnose remotely. An option would be to take your repo and put on a public GitHub repository so the code as a whole can be installed/looked at. You can also go backwards, removing features added until it works again and see if you can determine what is causing the error.

Sorry I can’t help more!


#13

Ok, Thank you so much for your help ! :)