sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Many issues with Django API boilerplate: multivaluedictkeyerror at /sign-up/ 'credentials' #90

Closed DRG104 closed 2 years ago

DRG104 commented 2 years ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

Django + react

What's the problem you're trying to solve?

Trying to utilize django_api_auth_boilerplate. When trying to view a mango (made by a super user) I receive 401: not authorized, no credentials provided. When I try to create a regular user to view (or create a mango) I receive multivaluedictkeyerror at /sign-up/ 'credentials'. The boilerplate is mostly stock, I only changed django_auth_template to our project name as per the set up instructions. I haven't attempted to add or create new models until I figure out mangos.

Post any code you think might be relevant (one fenced block per file)

Error message in browser points to: api.views.user_views.SignUp

class SignUp(generics.CreateAPIView):
    # Override the authentication/permissions classes so this endpoint
    # is not authenticated & we don't need any permissions to access it.
    authentication_classes = ()
    permission_classes = ()

    # Serializer classes are required for endpoints that create data
    serializer_class = UserRegisterSerializer

    def post(self, request):
        # Pass the request data to the serializer to validate it
        user = UserRegisterSerializer(data=request.data['credentials'])
        # If that data is in the correct format...
        if user.is_valid():
            # Actually create the user using the UserSerializer (the `create` method defined there)
            created_user = UserSerializer(data=user.data)

            if created_user.is_valid():
                # Save the user and send back a response!
                created_user.save()
                return Response({ 'user': created_user.data }, status=status.HTTP_201_CREATED)
            else:
                return Response(created_user.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(user.errors, status=status.HTTP_400_BAD_REQUEST)

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

Error message contains: MultiValueDictKeyError at /sign-up/ 'credentials'

copy and paste of traceback contains:

Request Method: POST
Request URL: http://127.0.0.1:8000/sign-up/

Django Version: 4.1
Python Version: 3.10.4
Installed Applications:
['api',
 'rest_framework',
 'rest_framework.authtoken',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'corsheaders']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last):
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\utils\datastructures.py", line 84, in __getitem__
    list_ = super().__getitem__(key)

During handling of the above exception ('credentials'), another exception occurred:
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\ph_ky\sei\django-env\project-4\server-dreamlancer\api\views\user_views.py", line 22, in post
    user = UserRegisterSerializer(data=request.data['credentials'])
  File "C:\Users\ph_ky\.virtualenvs\django-env-Kz-IbiFe\lib\site-packages\django\utils\datastructures.py", line 86, in __getitem__
    raise MultiValueDictKeyError(key)

Exception Type: MultiValueDictKeyError at /sign-up/
Exception Value: 'credentials'

What is your best guess as to the source of the problem?

I haven't got a clue, when I try to reuse the email used for my superuser during sign-up its obviously taken, so I don't think its an issue with the sign-up form. My super user can create, view and edit mangos, but since I can't create a regular user (with a different email of course) I can't really see what else the problem is. The boilerplate is being used as-is out of the box, so I'm worried there is something wrong with my work environment.

What things have you already tried to solve the problem?

I added a bearer token to the only user I have available (my super user) in case that was the credentials issue. I also went through the lessons related to the boilerplate and tried to create simple (test) view (that can't be displayed). I checked and rechecked import paths for the simple (test) view and checked import paths for the existing views as part of the boilerplate.

Paste a link to your repository here https://github.com/Zene09/server-dreamlancer

kestler01 commented 2 years ago

what does our request look like ?

DRG104 commented 2 years ago

The request is a simple POST:

Request Method: POST
Request URL: http://127.0.0.1:8000/sign-up/

image

kestler01 commented 2 years ago

marking as resolved with workaround being to manually input json via 'raw' option in the django browser form for sign up and sign in - ensuring that the request data has the 'credentials' key as per the routes already defined in the boiler plate