suraj sharma
suraj sharma

Reputation: 413

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)

Upvotes: 2

Views: 2170

Answers (2)

Nafees Anwar
Nafees Anwar

Reputation: 6608

Class based views do not work this way. You have to define a method for every http method type you want to cover (At least if you are simply inheriting from View) class. So define a method in your class based view for post like this and it will work

class UrlAccpt(View):

    template_name='phc/url_accpt.html'

    def get(self,request):
        urlx=''
        form = UrlForm()

   def post(self,request, *args, **kwargs):
        form = UrlForm(request.POST)
        if form.is_valid():
            urlx= form.cleaned_data['EnterTheUrl']

You can read about it in Supporting other HTTP methods of this doc

Upvotes: 1

webbyfox
webbyfox

Reputation: 1069

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

Upvotes: 3

Related Questions