Search code examples
djangodjango-formsdjango-viewsdjango-2.1

Method Not Allowed: / (Django-Post Method)


When I submit the form, it says "Method Not Allowed: /" in the console..

Something like that: Method Not Allowed: / [17/Mar/2019 18:31:18] "POST / HTTP/1.1" 405


I'm using this on views.py file..

class UrlAccpt(View):

    template_name='phc/url_accpt.html'

    def get(self,request):
        urlx=''
        form = UrlForm(request.POST)
        if request.method == 'POST':
            form = UrlForm(request.POST)
            if form.is_valid():
                urlx= form.cleaned_data['EnterTheUrl']

        form = UrlForm(request.POST)

    response = TemplateResponse(request,self.template_name,{'form':form,'value':urlx})
    return response

and in forms.py file...I use this code

from django import forms


class UrlForm(forms.Form):

    EnterTheUrl=forms.CharField(max_length=1000)


Solution

  • Welcome to class based views:

    You need to specify post function in your class. Get function only triggered on GET method, not for POST request.

    Add following function and move your post logic here...

    def post:
       ...
    

    Have a look docs