2

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)

AArias
  • 2,558
  • 3
  • 26
  • 36
suraj sharma
  • 415
  • 4
  • 14

2 Answers2

3

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

webbyfox
  • 1,049
  • 10
  • 22
1

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

Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42