[Answered ]-Parsing POST request – object() takes no parameters

1πŸ‘

βœ…

In this case better to use function in the url

from your_app.views import check

url(r'^api/checkmail/', check, name='check')

and your view will be like this (only function)

@csrf_exempt
def check(request):
  email = request.POST.get("email")
  if User.objects.filter(email=email).exists():
     return JsonResponse({'status': 'not available'})

Also FYI if you want to use @csrf_exempt with classes you should use dispatch, you can get more info here

Example of JsonResponse

from django.http import JsonResponse
def your_view(request):
    return JsonResponse({'foo':'bar'})

1πŸ‘

You need to inherit your class your generic django views for this to work. Url patterns expect a callable that can accept request (with args and kwargs) and return response. View.as_view class method returns a callable for you.

# In views file
from django.views.generic import View

...

class CheckIfEmailAvailable(View):

      def get(self, request, *args, **kwargs):
          return self.check(request) # or something else depends on your use case

# In urls file
from django.views.decorators.csrf import csrf_exempt
...
url(r'^api/checkmail/', csrf_exempt(CheckIfEmailAvailable.as_view()), name='check'),

Moreover csrf_exempt decorator won’t work (out of box) on classes, neither on bound methods for that matter. Better way should be to use them in urls.

πŸ‘€hspandher

0πŸ‘

If you had posted the full traceback, you would see that the error does not come from parsing the request; Django does not even get nearly that far.

You can’t use a class as a view like that. You should have check as a standalone function, without a class, and refer to it directly in your urls.

Django does support class-based views, but they have a very specific structure, need to inherit from the proper base class, and be referenced in the urls.py in a specific way.

Leave a comment